【问题标题】:Error sending json in POST to web API service将 POST 中的 json 发送到 Web API 服务时出错
【发布时间】:2014-04-18 12:36:53
【问题描述】:

我正在使用 Web API 创建一个 Web 服务。我实现了一个简单的类

public class ActivityResult
{
    public String code;
    public int indexValue;
    public int primaryCodeReference;
}

然后我在我的控制器内部实现了

[HttpPost]
public HttpResponseMessage Post(ActivityResult ar)
{
    return new HttpResponseMessage(HttpStatusCode.OK);
}

但是当我调用 API 时传入 POST 文件 json:

{"code":"XXX-542","indexValue":"3","primaryCodeReference":"7"}

我收到以下错误消息:

{
    "Message": "The request entity's media type 'text/plain' is not supported for this resource.",
    "ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'ActivityResult' from content with media type 'text/plain'.",
    "ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
    "StackTrace": "   in System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   in System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   in System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
}

我做错了什么?

【问题讨论】:

  • 您必须添加“application/json”标头才能从客户端接受有效负载。
  • 我已经在我的 HTTP 请求中正确设置了标头。但是问题似乎出在服务器端:dropbox.com/s/xlidnnybs8v6d0u/Cattura.JPG
  • 看起来您只是将Accept 标头设置为application/json。您还需要将Content-Type 标头设置为application/json

标签: c# asp.net .net json asp.net-web-api


【解决方案1】:
  1. 你必须添加标题属性Content-Type:application/json
  2. 当您定义任何应注释为[FromBody] 的POST 请求方法输入参数时,例如

    [HttpPost]
    public HttpResponseMessage Post([FromBody]ActivityResult ar)
    {
      return new HttpResponseMessage(HttpStatusCode.OK);
    }
    
  3. 任何 JSON 输入数据都必须是原始数据。

【讨论】:

    【解决方案2】:

    如果不提及任何内容,则需要在 web api 请求标头部分中包含Content-Type:application/json,然后默认为Content-Type:text/plain 传递给请求。

    在邮递员工具上测试 api 的最佳方式。

    【讨论】:

      【解决方案3】:

      我的所有设置都包含在接受的答案中。 我遇到的问题是我试图更新实体框架实体类型“任务”,例如:

      public IHttpActionResult Post(Task task)
      

      对我有用的是创建我自己的实体“DTOTask”,例如:

      public IHttpActionResult Post(DTOTask task)
      

      【讨论】:

        【解决方案4】:

        请检查您是否以POST 而不是GET 传递方法。 如果是这样,您将收到与上面发布的相同的错误。

        $http({               
         method: 'GET',
        

        请求实体的媒体类型“text/plain”不支持 这个资源。

        【讨论】:

        • 这个问题具体是关于一个http POST,他没有从服务器请求数据,而是向服务器发送数据。
        【解决方案5】:

        另一个提示...将“content-type: application/json”...添加到 Composer/Parsed 选项卡上的文本框字段的位置。那里已经填写了 3 行,所以我将此 Content-type 添加为第 4 行。这使得 Post 工作。

        【讨论】:

          【解决方案6】:

          在 HTTP 请求中你需要设置 Content-Type 为:Content-Type: application/json

          因此,如果您使用 fiddler 客户端,请将 Content-Type: application/json 添加到请求标头中

          【讨论】:

            猜你喜欢
            • 2018-05-07
            • 2021-07-08
            • 2021-08-04
            • 1970-01-01
            • 1970-01-01
            • 2014-10-03
            • 1970-01-01
            • 1970-01-01
            • 2016-12-28
            相关资源
            最近更新 更多