3

I'm having problems with returning a 'text/plain' json response from WebApi.
I need to return it as text/plain and by default it returns it as application/json.
The problem I have is exactly what happens in this link. This isn't a duplicate post since I've read multiple posts but still have a small problem.

Here's my server code:

public HttpResponseMessage Post()
{
       var response = GetModel();
       string jsonRes = JsonConvert.SerializeObject(response);

       var resp = new HttpResponseMessage();
       resp.Content = new StringContent(jsonRes);
       resp.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");

       return resp;
}

This is the response I get:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
  Content-Type: text/plain
}

The jsonRes var contains a valid JSON.
Why is the Content downloaded as Content: System.Net.Http.StringContent?

I'm probably missing something small here.

Community
  • 1
  • 1
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • possible duplicate of [Is there a way to force ASP.NET Web API to return plain text?](http://stackoverflow.com/questions/11581697/is-there-a-way-to-force-asp-net-web-api-to-return-plain-text) – Erti-Chris Eelmaa Jul 21 '14 at 09:46
  • You're returning an object of type `HttpResponseMessage`, not setting any variables for the actual content type. – Sami Kuhmonen Jul 21 '14 at 09:46
  • How you call this API ? $.ajax (jQuery) has a datatype to set the good type to return. – User.Anonymous Jul 21 '14 at 09:46
  • @Erti-ChrisEelmaa This isn't a duplicate. I've read 20 post's and I still have a problem. – Amir Popovich Jul 21 '14 at 09:47
  • @SamiKuhmonen - I'm setting the content type as you can see here: resp.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); – Amir Popovich Jul 21 '14 at 09:48
  • @User.Anonymous - I have a problem as stated in this link: http://stackoverflow.com/questions/8151138/ie-jquery-form-multipart-json-response-ie-tries-to-download-response .. I'm using the dataType:'text' instead of json. – Amir Popovich Jul 21 '14 at 09:49
  • Did you seen with Fiddler what is really sent by server ? And this new StringContent("foo", Encoding.UTF8, "text/plain"); is not working better ? – User.Anonymous Jul 21 '14 at 09:57
  • @User.Anonymous - I've added Fiddler's response to the server and it's not an encoding problem. sylwester's answer helped me. – Amir Popovich Jul 21 '14 at 10:26

2 Answers2

5

You can do that in this way:

  public string Post()
    {
           var response = GetModel();
           string jsonRes = JsonConvert.SerializeObject(response);


           return jsonRes ;
    }
sylwester
  • 16,498
  • 1
  • 25
  • 33
  • 1
    Wow..Didn't even think of that since all the posts on the web sticked to HttpResponseMessage. That did the job..Thanks! – Amir Popovich Jul 21 '14 at 09:55
2

The original question is a bit old, but I had the same problem today, so maybe someone else will be looking for a solution. This works for me:

public HttpResponseMessage Post()
{
   var response = GetModel();
   string jsonRes = JsonConvert.SerializeObject(response);

   var resp = new HttpResponseMessage();
   resp.Content = new StringContent(jsonRes, System.Text.Encoding.UTF8, "application/json");

   return resp;
}

So the "something small" you mentioned is the mediaType parameter in StringContent constructor:

public StringContent (string content, System.Text.Encoding encoding, string mediaType);
skan
  • 81
  • 1
  • 6