Null value when Pass values [FromBody] to post method by Postman plugin [duplicate]
I use api controller in ASP.net web API and i need to pass value to post method by [FromBody] type..
[HttpPost]
public HttpResponseMessage Post( [FromBody]string name)
{
....
}
i use Postman plugin but when send to post method value of name always is null.. follow this image:
and in Post methods :
why this happend?!
Solution 1:
Post the string with raw json, and do not forget the double quotation marks.
Solution 2:
You can't bind a single primitive string using json and FromBody, json will transmit an object and the controller will expect an complex object (model) in turn. If you want to only send a single string then use url encoding.
On your header set
Content-Type: application/x-www-form-urlencoded
The body of the POST request message body should be =saeed
(based on your test value) and nothing else. For unknown/variable strings you have to URL encode the value so that way you do not accidentally escape with an input character.
Alternate 1
Create a model and use that instead.
Message body value: {"name":"saeee"}
c#
public class CustomModel {
public string Name {get;set;}
}
Controller Method
public HttpResponseMessage Post([FromBody]CustomModel model)
Alternate 2
Pass primitive strings to your post using the URI instead of the message body.