Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Content-Type header be present when the message body is empty?

Should the Content-Type header be present in an HTTP request or response when there is no payload body?

Is the correct combination of HTTP headers in this case to have no Content-Type and a Content-Length of 0, or should the Content-Type not be present at all when the message lacks a body?

like image 584
pmckeown Avatar asked Apr 21 '15 22:04

pmckeown


People also ask

What happens if there is no Content-Type header?

If a Content-Type header field is not present, the recipient MAY either assume a media type of "application/octet-stream" ([RFC2046], Section 4.5. 1) or examine the data to determine its type. It means that the Content-Type HTTP header should be set only for PUT and POST requests.

Is Content-Type header necessary?

In short, no, it's not required.

What is the purpose of Content-Type header?

The Content-Type representation header is used to indicate the original media type of the resource (prior to any content encoding applied for sending). In responses, a Content-Type header provides the client with the actual content type of the returned content.

Can Content-Type empty?

Instead, browsers can just reuse the cache stored in the browser to display the content to the user. In this case, since the HTTP 304 response doesn't transmit any content back to the browser, this also means that there will be no response body and leading to “Empty” content type.


1 Answers

RFC 7231 says:

A sender that generates a message containing a payload body SHOULD generate a Content-Type header field in that message [...]

So it doesn't say explicitly what to do if you're generating a message that doesn't have a payload body, but it certainly seems implied that not sending a Content-Type header would be appropriate in that case.

Content-Length: 0 is how you would indicate to the receiver that there will be no body, unless you're using something like chunked encoding.

like image 57
DrGoldfire Avatar answered Sep 21 '22 13:09

DrGoldfire