发送json到web API服务时出错

时间:2021-09-16 19:35:28

I'm creating a web service using Web API. I implemented a simple class

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

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

And then I have implemented inside my controller

然后在控制器中实现

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

But when I call the API passing in POST the file json:

但是当我把API传递到POST文件json时:

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

I obtain the following error message:

我得到以下错误信息:

{
    "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)"
}

What am I doing wrong?

我做错了什么?

6 个解决方案

#1


161  

In the HTTP request you need to set Content-Type to: Content-Type: application/json

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

So if you're using fiddler client add Content-Type: application/json to the request header

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

#2


1  

another tip...where to add "content-type: application/json"...to the textbox field on the Composer/Parsed tab. There are 3 lines already filled in there, so I added this Content-type as the 4th line. That made the Post work.

另一个提示…在哪里添加“content-type: application/json”…到Composer/Parsed选项卡上的文本框字段。这里已经填满了3行,所以我把这个Content-type作为第4行。这使得邮报得以运作。

#3


0  

Please check if you are were passing method as POST instead as GET. if so you will get same error as a you posted above.

请检查您是否正在传递方法作为POST代替GET。如果是这样的话,你将会得到与你上面发布的相同的错误。

$http({               
 method: 'GET',

The request entity's media type 'text/plain' is not supported for this resource.

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

#4


0  

I had all my settings covered in the accepted answer. The problem I had was that I was trying to update the Entity Framework entity type "Task" like:

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

public IHttpActionResult Post(Task task)

What worked for me was to create my own entity "DTOTask" like:

为我工作的是创建我自己的实体“DTOTask”,比如:

public IHttpActionResult Post(DTOTask task)

#5


0  

  1. You have to must add header property Content-Type:application/json
  2. 必须添加header属性Content-Type:application/json
  3. When you define any POST request method input parameter that should be annotated as [formbody], e.g.:

    当您定义任何POST请求方法的输入参数时,应该将其注释为[formbody],例如:

    [HttpPost]
    public HttpResponseMessage Post([formbody]ActivityResult ar)
    {
      return new HttpResponseMessage(HttpStatusCode.OK);
    }
    
  4. Any JSON input data must be raw data.

    任何JSON输入数据都必须是原始数据。

#6


0  

It require to include Content-Type:application/json in web api request header section when not mention any content then by default it is Content-Type:text/plain passes to request.

它要求在web api请求头部分包含content - type:application/json,当不涉及任何内容时,默认情况下是content - type:text/plain传递到request。

Best way to test api on postman tool.

在邮差工具上测试api的最佳方法。

#1


161  

In the HTTP request you need to set Content-Type to: Content-Type: application/json

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

So if you're using fiddler client add Content-Type: application/json to the request header

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

#2


1  

another tip...where to add "content-type: application/json"...to the textbox field on the Composer/Parsed tab. There are 3 lines already filled in there, so I added this Content-type as the 4th line. That made the Post work.

另一个提示…在哪里添加“content-type: application/json”…到Composer/Parsed选项卡上的文本框字段。这里已经填满了3行,所以我把这个Content-type作为第4行。这使得邮报得以运作。

#3


0  

Please check if you are were passing method as POST instead as GET. if so you will get same error as a you posted above.

请检查您是否正在传递方法作为POST代替GET。如果是这样的话,你将会得到与你上面发布的相同的错误。

$http({               
 method: 'GET',

The request entity's media type 'text/plain' is not supported for this resource.

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

#4


0  

I had all my settings covered in the accepted answer. The problem I had was that I was trying to update the Entity Framework entity type "Task" like:

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

public IHttpActionResult Post(Task task)

What worked for me was to create my own entity "DTOTask" like:

为我工作的是创建我自己的实体“DTOTask”,比如:

public IHttpActionResult Post(DTOTask task)

#5


0  

  1. You have to must add header property Content-Type:application/json
  2. 必须添加header属性Content-Type:application/json
  3. When you define any POST request method input parameter that should be annotated as [formbody], e.g.:

    当您定义任何POST请求方法的输入参数时,应该将其注释为[formbody],例如:

    [HttpPost]
    public HttpResponseMessage Post([formbody]ActivityResult ar)
    {
      return new HttpResponseMessage(HttpStatusCode.OK);
    }
    
  4. Any JSON input data must be raw data.

    任何JSON输入数据都必须是原始数据。

#6


0  

It require to include Content-Type:application/json in web api request header section when not mention any content then by default it is Content-Type:text/plain passes to request.

它要求在web api请求头部分包含content - type:application/json,当不涉及任何内容时,默认情况下是content - type:text/plain传递到request。

Best way to test api on postman tool.

在邮差工具上测试api的最佳方法。