0

I have a basic API call that takes in two id's and adds them to a favorites matrix of users to profiles. I've set the call up in C# MVC like so:

[Route("api/discovery/addToFavourite/{profileid}/{userid}")]
public void AddToFav(int profileid, int userid)
{
    var Favourite = new Favourite();
    Favourite.ProfileId = profileid;
    Favourite.UserId = userid;

    WildWalkDb.Favourites.Add(Favourite);
    WildWalkDb.SaveChanges();
}

And then from Angular 2 and TypeScript I have:

addToFav(profileid) {
    var headers = new Headers();
    headers.append('Content-Type', 'application/json');
    let options = new RequestOptions({ headers: headers });
    return this.http.post(this.url + '/api/discovery/favourite/' + profileid + '/' + 1)
    .toPromise()
    .then(response => <string>response.statusText)
    .catch(this.handleError);
  }

I just need to run that url with the parameters without passing any Json or anything over, but this fails, presumably because it's missing additional parameters on the http.post call. So from all this I'm guessing this isn't the best way to do this and maybe I'm majorly missing the point of how post calls work? What's the best way for me to just make that api call?

3
  • The route, and post URL don't match. The API function is not using the HttpPost attribute, but that may not matter. Errors in the console? Any debug errors? Commented May 21, 2017 at 18:31
  • if you open the network tabs on your browser, do you see that from the client side the request starts correctly? are you catching the error? can you describe a bit better what's your current issue with? then your request seems to be directed to another route Commented May 21, 2017 at 18:32
  • I know what the error is - I posted it above ('because it's missing additional parameters on the http.post call.') - what I need to know is the correct way to do this as I don't have an object to pass in as the additional parameter Commented May 21, 2017 at 18:41

1 Answer 1

1

If the parameters are not required, make them optional then at runtime fill/provide them, otherwise provide them at call point.

--(Edit) Provide empty values at call point.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah I know that - but I don't need to fill them as I just need to run the url but they're not optional, so I need to know the best way to make that post call that doesn't need me to pass in those parameters.
If they're not optional why don't you just provide an empty value for them?
Didn't actually know I could do that - it worked though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.