OK, assuming your responseData is a string, the Content-type header will be text/plain when creating the HttpResponseMessage . It does not 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 return media type:
HttpResponseMessage response = new HttpResponseMessage() { Content = new StringContent( responseData, Encoding.UTF8, "application/json" ) };
There are other methods that return a specific type of object and allow the API libraries to be serialized in JSON or XML for you as needed. I prefer the infrastructure to do the work for me wherever possible, but this is how you will achieve this with the line you created yourself.
For a rigorous JSON-only result, remove the XML formatter from the WebAPI configuration and return 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 and requested /api/Test from Chrome, which doesn't even mention application/json in the Accept header. Here are the response headers until they reach the 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 was JSON by default.