Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API 2 returning text/plain responses

Really struggling with something I hope people here can help with. I'm writing a RESTful API in Web API 2. Whenever I send a request to this service, the response is consistently being sent with a Content-Type of text/plain. Obviously this is no good, my response needs to be Content-Type of application/json. I've tried a few suggestions that I found from Google but I don't think I'm understanding the whole picture.

Is there something special I need to do in order to have my web service respond with application/json content? Note that I want this to work globally across the whole app, so I'm not after a way to modify a given response and set its content type - I want this to be a default behaviour for the whole web service: If a request contains an Accept header for application/json I want my web service to return that Content-Type instead of text/plain.

Edit to clarify:

My response contains an object called "responseData" that should be serialized into JSON and included in the body. I'm currently putting together my response like this:

HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, responseData);
return response;

responseData is a POCO. This get's correctly serialized as JSON and returned in the response - the only missing part is the Content-Type which is incorrectly set to "text/plain". I could manually change this on every single response I compose, but I'm wanting to configure this on a global level.

like image 697
Nick Coad Avatar asked Jun 20 '14 05:06

Nick Coad


People also ask

How do I return a message in Web API?

Depending on which of these is returned, Web API uses a different mechanism to create the HTTP response. Convert directly to an HTTP response message. Call ExecuteAsync to create an HttpResponseMessage, then convert to an HTTP response message. Write the serialized return value into the response body; return 200 (OK).

Which of the following types are valid response types of Web API 2 action method?

A Web API 2 action method return types can be any of the following : Void. HttpResponseMessage. IHttpActionResult.

What format does Web API return data to?

By default Web API returns result in XML format.

What does IHttpActionResult return?

IHttpActionResult ultimately returns HttpResponseMessage by providing the customization and reusability features to the developer. IHttpActionResult contains ExecuteAsync method to create an instance of HttpResponseMessage asynchronously. We have to add some code logic in the implemented class as per our requirement.


1 Answers

OK, assuming that your responseData is a string, the Content-type header will be text/plain when you create the HttpResponseMessage. Doesn't matter what the contents of the string are, since no attempt is made to determine this.

The solution is to create the appropriate Content object for your message, initialized with the media type you're returning:

HttpResponseMessage response = new HttpResponseMessage()
    {
        Content = new StringContent(
                responseData,
                Encoding.UTF8,
                "application/json"
            )
    };

There are other methods that amount to returning a particular object type and letting the API libraries serialize to JSON or XML for you as required. I prefer to let the framework do the work for me where possible, but this is how you'd achieve it with a string you've created yourself.


For a strict JSON-only result, remove the XML formatter from the WebAPI configuration and return your POCO.

In App_Start\WebApiConfig.cs, add the following to the WebApiConfig.Register method:

config.Formatters.Remove(config.Formatters.XmlFormatter);

And for your API:

public class MyObject
{
    public bool result;
    public string reason;
}

public class TestController : ApiController
{
    public MyObject GetData()
    {
        MyObject result = new MyObject { result = "true", reason = "Testing POCO return" };
        return result;
    }
}

I ran this up and requested /api/Test from Chrome, which doesn't even mention application/json in the Accept header. Here are the response headers up until it hits Content-Type:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8

And the body:

{"result":true,"reason":"Testing POCO return"}

Since I disabled XML it defaulted to JSON.

like image 153
Corey Avatar answered Sep 25 '22 03:09

Corey