Error posting JSON to Web API 2 : The request enti

2020-07-02 18:18发布

I have this class :

public class MyClass
{
    public MyClass() { Secret = "Don't tell me"; }
    public string Name { get; set; }
    public int Age { get; set; }
    public string Description { get; set; }
    private string Secret { get; set; }
}

And this WEB API method :

        // POST api/fixture
    public HttpResponseMessage Post(MyClass value)
    {
        return new HttpResponseMessage(HttpStatusCode.Created);
    }

I've set Web API to return JSON instead of XML and I haven't done any other change to the default config. I'm trying to test this method with the RESTClient extension of Firefox. Here is my request :

POST localhost:XXXX/api/fixture
Content-Type: application/json
Accept: application/json
Content-Length: 60

{
 value : { "Name":"Cosby","Age":"13","Description":"OK" }
}

However I'm getting this error :

{"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 'MyClass' from content with media type 'text/plain'.","ExceptionType":"System.Net.Http.UnsupportedMediaTypeException","StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"}

Edit:

I don't understand because it seems that the method is not even called. If I debug, I see that the constructor is called and then no other method is called. No exception is raised.

I've been on this issue for a lot of time now. I've found that this problem usually happens when Content-Type is not set properly but it doesn't seem to be my case since the request is not even processed.

Any idea ?

Thanks

5条回答
贼婆χ
2楼-- · 2020-07-02 18:32

I got this problem when I had a controller that had two different models in the params. The Post method was using a model that was not specified in the builder.EntitySet The way I solved this is to create a separate read and write controllers. This is along the lines of CQRS. Although the API specs become more messy.. thank god for Swagger!

查看更多
狗以群分
3楼-- · 2020-07-02 18:35

You were sending the content-type of application/json in the body, rather than as a header. So your POST request was defaulting to text/plain. The RestClient extension has a separate place to enter the header.

If you ever have a question about what's being sent over the wire, check the Network tab in your browser's developer tools, or use a tool such as Fiddler to see the network traffic.

查看更多
smile是对你的礼貌
4楼-- · 2020-07-02 18:46

Probably you need to add following contents in your HTTP request header:

Content-Type: application/json
查看更多
霸刀☆藐视天下
5楼-- · 2020-07-02 18:52

Use Postman to test your Web API, configure it to have header: content-type: application/json Since that is a POST request, you have to put the raw json data.

查看更多
该账号已被封号
6楼-- · 2020-07-02 18:54

Inside POSTMAN, you only need to change the setting to application/json. Refer image below.

application/json

查看更多
登录 后发表回答