I am on Dotnet 3.0.103. I have exposed a webapi, that has a code defined like this,
[HttpPost]
public string Post([FromBody] string data)
{
try
{
EmployeeRequest request = JsonConvert.DeserializeObject<EmployeeRequest>(data);
Now, when I call the API from my postman, a normal json request body like this, does not work
{"EmployeeId": 3, "Name": "John"}
It throws an error like this,
{
"errors": {
"": [
"Unexpected character encountered while parsing value: {. Path '', line 1, position 1."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|b9ea70ff-4ffda7ef1c244839."
}
Where as an escaped string as opposed to a Json body works fine,
"{\"EmployeeId\": 3, \"Name\": \"John\"}"
I would like to make it work with just a plain json.
Any help is appreciated.
Thanks, Arun
ASP.Net's model binding can bind a JSON payload to your Action's parameters automatically. As such you should be able to do away with the manual deserialization:
[HttpPost]
public string Post([FromBody]EmployeeRequest request)
{
// request should be properly populated