Search code examples
c#jsonmodel-view-controllerasp.net-web-apifiddler

[FromBody]JToken param is null when ContentType: application/json or it's bad serialized when Contenttype: application/x-www-form-urlencoded [Fiddler]


As the topic says, I've faced with a problem while trying to call an endpoint (POST) with a Request Body via Fiddler.

There is a http api method:

[HttpPost]
[Route("books")]
 public IHttpActionResult GetBooks([FromBody]JToken filterRequest)
 {...}

In Fiddler I make a POST call with request header:

User-Agent: Fiddler Host: localhost Content-Length: 21 Content-Type: application/json

And the Request Body looks like: { "title" : "Harry Potter"}. While debugging it, I get null filterRequest parameter.

But when I use Content-Type: application/x-www-form-urlencoded, parameter filterRequest parameter is not null but unfortunately contains incorrect syntax like:

{{"{ \"title\" : \"Harry Potter\"}": ""}}

And it's wrong serialized by:

var columnsFilter = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(filter);

I have no idea what I am doing wrong here. Maybe do you have met with similar problem and could help?


Solution

  • It turned out that the whole problem was based on wrong quotes. I had to use just ' and the posted request body was correct with Content-Type: application/json.