【问题标题】:[FromBody]JToken param is null when ContentType: application/json or it's bad serialized when Contenttype: application/x-www-form-urlencoded [Fiddler][FromBody]JToken param is null when ContentType: application/json or it's bad serialized when Contenttype: application/x-www-form-urlencoded [Fiddler]
【发布时间】:2016-12-12 13:49:25
【问题描述】:

正如主题所说,我在尝试通过 Fiddler 调用带有请求正文的端点 (POST) 时遇到了问题。

有一个http api方法:

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

在 Fiddler 中,我使用请求标头进行 POST 调用:

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

请求正文看起来像:{ "title" : "Harry Potter"}。在调试它时,我得到了 null filterRequest 参数。

但是当我使用Content-Type: application/x-www-form-urlencoded 时,参数 filterRequest 参数 不为空,但不幸的是包含不正确的语法,例如:

{{"{ \"title\" : \"哈利波特\"}": ""}}

而且是错误的序列化方式:

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

我不知道我在这里做错了什么。也许您遇到过类似的问题并可以提供帮助?

【问题讨论】:

    标签: c# json model-view-controller asp.net-web-api fiddler


    【解决方案1】:

    原来整个问题是基于错误的引用。我只需要使用 ' 并且发布的请求正文与 Content-Type: application/json.

    是正确的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-11
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 2016-04-13
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多