Reputation: 2261
So I am trying to pass a string to my Web Api through a put call in an Angular service. The service actually calls the method on the Web Api side, but the parameter always comes through null. Can someone shed a bit of light, thanks.
Here is what the code looks like:
Web Api: (the breakpoint is set on the first parenthesis)
public void Put([FromBody]string userId)
{
}
Angular :
UpdateManagerEmail(userId){
userId = '577f1e6e-146d-49f1-a354-b31349e7cb49';
const headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
this._http.put('http://localhost:428/api/User', {
userId : userId
}).subscribe(returnVal => {
console.log('returned');
});
}
Upvotes: 0
Views: 50
Reputation: 5426
For your backend to find userId in the request body you have send it needs to be the only value in there. You should change the content-type
to: application/x-www-form-urlencoded; charset=UTF-8
and send the data without an key.
Remark on the docs:
Before sending a simple type, consider wrapping the value in a complex type instead. This gives you the benefits of model validation on the server side, and makes it easier to extend your model if needed.
Source: Sending simple types
Also, this blog explains it better than I do.
Upvotes: 1