Error sending JSON in Web API 2: message carrier type "text / plain" is not supported for this resource

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 installed the Web API to return JSON instead of XML, and I did not make any other changes to the default configuration. I am 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 get this error:

{"Message": "The media type of the message" text / plain "is not supported for this resource.", "ExceptionMessage": "No MediaTypeFormatter is available for reading an object of type MyClass from the contents with the environment, enter" 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 at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable 1 formatters, IFormatterLogger formatterLogger, CancellationToken cancelationToken) \ r \ n in System.Web.Http.ModelBinding.FormatterParameterBindingCessRead.eadRestReadRead.readTestReadRead.eadRead.eadmentead IEnumerable`1 formatters, IFormatterLogger formatterLogg er, CancellationToken cancelationToken) "}

Edit:

I do not understand, because it seems that the method is not even called. If I debug, I see that the constructor is being called, and then no other method is being called. There are no exceptions.

I have been dealing with this problem for many years. I found that this problem usually occurs when the Content-Type is set incorrectly, but this does not seem to be my case, since the request is not even processed.

Any idea?

thanks

+7
json asp.net-web-api asp.net-web-api2
source share
5 answers

You sent the application/json content type to the body, not as a header. So your POST request did not execute text / plain. The RestClient extension has a separate place for entering the header.

If you have a question about what is sent via cable, check the Network tab in your browser’s developer tools or use a tool like Fiddler . to view network traffic.

+9
source share

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

application / json

+9
source share

Use Postman to test your web API, set it to the header: content-type: application / json. Since this is a POST request, you need to put raw json data.

+3
source share

Perhaps you need to add the following content to the HTTP request header :

 Content-Type: application/json 
+1
source share

I had this problem when I had a controller with two different parameters in the parameters. The Post method used a model that was not specified in builder.EntitySet . I decided that this was to create separate read and write controllers. This complies with CQRS. Although the API specs are getting messier. Thank god for the Swagger!

0
source share

All Articles