The http spec says about the HEAD
request:
The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request.
Should the response to a HEAD
request contain a Content-Length
header? Should it be the value which would be returned on a GET
request, even if there is no response body? Or should the Content-Length be 0?
Yes, the
Content-Length
of aHEAD
response SHOULD, but not always does (see @Paul's answer) include theContent-Length
value of aGET
response:Stack Overflow does:
Google doesn't:
Section 14.13 of the HTTP/1.1 spec detailed the Content-Length header, and says this:
The word 'SHOULD' has a very specific meaning in RFCs:
So, you may not always see a Content-Length. Typically you might not see it for any content which is dynamically generated, since that might be too expensive to service an exploratory HEAD request. For example, a HEAD request to Apache for a static file will have a Content-Length, but a request for a PHP script may not.
For example, try this very website...
No content-length there.
Contra the accepted answer, section 4.3.2 of RFC 7231 states:
—which is to say, Content-Length, Content-Range, Trailer, and Transfer-Encoding—
This is even weaker than the note on SHOULD in Paul Dixon's answer:
So the real answer is, you don't need to include Content-Length, but if you do, you should give the correct value.
To me it looks like the HTTP 1.1 RFC is pretty specific:
The HTTP-spec at W3C states:
Which (to me) means it should hold the "correct" value as you would in a
GET
response.