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?
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.
In short, no, it's not required.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With