Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error posting JSON to Web API 2 : The request entity's media 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'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

like image 539
tobiak777 Avatar asked Sep 19 '14 13:09

tobiak777


4 Answers

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

application/json

like image 183
Azlan Jamal Avatar answered Nov 14 '22 13:11

Azlan Jamal


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.

like image 22
mason Avatar answered Nov 14 '22 13:11

mason


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

Content-Type: application/json
like image 11
Baqer Naqvi Avatar answered Nov 14 '22 11:11

Baqer Naqvi


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.

like image 4
Jayson Avatar answered Nov 14 '22 12:11

Jayson