Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set Content-Type header on HttpResponseMessage headers?

I'm using the ASP.NET WebApi to create a RESTful API. I'm creating a PUT method within one of my controllers, and the code looks like this:

public HttpResponseMessage Put(int idAssessment, int idCaseStudy, string value) {     var response = Request.CreateResponse();     if (!response.Headers.Contains("Content-Type")) {         response.Headers.Add("Content-Type", "text/plain");     }      response.StatusCode = HttpStatusCode.OK;     return response; } 

When I PUT to that location with the browser via AJAX, it gives me this Exception:

Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

But isn't Content-Type a perfectly valid header for a response? Why am I getting this exception?

like image 614
Jez Avatar asked Nov 14 '12 11:11

Jez


People also ask

How do I set HttpResponseMessage content?

Several months ago, Microsoft decided to change up the HttpResponseMessage class. Before, you could simply pass a data type into the constructor, and then return the message with that data, but not anymore. Now, you need to use the Content property to set the content of the message.

How do I add content length to my header?

To manually pass the Content-Length header, you need to add the Content-Length: [length] and Content-Type: [mime type] headers to your request, which describe the size and type of data in the body of the POST request.

What should I return to HttpResponseMessage?

HttpResponseMessage works with HTTP protocol to return the data with status/error.


2 Answers

Have a look at the HttpContentHeaders.ContentType Property:

response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); 

if (response.Content == null) {     response.Content = new StringContent("");     // The media type for the StringContent created defaults to text/plain. } 
like image 134
dtb Avatar answered Sep 20 '22 14:09

dtb


Something is missing in ASP Web API: the EmptyContent type. It will allow sending an empty body while still allowing all content-specific headers.

Put the following class somewhere in your code :

public class EmptyContent : HttpContent {     protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)     {         return Task.CompletedTask;     }     protected override bool TryComputeLength(out long length)     {         length = 0L;         return true;     } } 

Then use it as you wish. You now have a content object for your extra headers.

response.Content = new EmptyContent(); response.Content.Headers.LastModified = file.DateUpdatedUtc; 

Why use EmptyContent instead of new StringContent(string.Empty)?

  • StringContent is a heavy class that executes lots of codes (because it inherits ByteArrayContent)
    • so let's save a few nanoseconds
  • StringContent will add an extra useless/problematic header: Content-Type: plain/text; charset=...
    • so let's save a few network bytes
like image 31
SandRock Avatar answered Sep 20 '22 14:09

SandRock