Web API 2 returning text/plain responses

19,403

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.

Share:
19,403
Nick Coad
Author by

Nick Coad

Updated on July 13, 2022

Comments

  • Nick Coad
    Nick Coad almost 2 years

    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.

  • Nick Coad
    Nick Coad almost 10 years
    As mentioned in my question, I'm trying to find a global way to set this up, rather than having to explicitely define the Content Type for every response I create. My wording was a bit unclear perhaps - I've edited my OP to hopefully clarify a couple of points.
  • Corey
    Corey almost 10 years
    Have you tried just returning the object itself? HttpResponseMessage is fine when you want more control over individual returns, but you can return a POCO and let the libraries take care of serialization for you - JSON or XML, depending on the Accept header in the request.
  • Nick Coad
    Nick Coad almost 10 years
    I suppose the problem is that I'll still have a need to return 404s or 500s etc if something goes wrong - is that still possible using the described approach?
  • Corey
    Corey almost 10 years
    Yes, by throwing an HttpResponseException.
  • Suamere
    Suamere over 9 years
    This is assuming somebody has built a JsonNetFormatter of some sort, and doesn't work without that understanding or more detail.