18
votes

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
You don't need to obfuscate your port number in your question. We don't know where localhost is. Anyways, are you positive that your exception occurs with that POST message? The exception makes it sound like you're posting a content type of text/plain rather than application/json.mason

5 Answers

19
votes

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.

34
votes

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

application/json

10
votes

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

Content-Type: application/json
4
votes

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.

0
votes

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!