http 规范 介绍了 HEAD< /代码> 请求:
HEAD 方法与 GET 相同,只是服务器不得在响应中返回消息正文。响应 HEAD 请求的 HTTP 标头中包含的元信息应该与响应 GET 请求而发送的信息相同。
对 HEAD
请求的响应是否应该包含 Content-Length
标头?即使没有响应正文,它是否应该是 GET
请求返回的值?或者内容长度应该为0?
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?
发布评论
评论(6)
对我来说,HTTP 1.1 RFC 很漂亮具体的:
To me it looks like the HTTP 1.1 RFC is pretty specific:
HTTP/1.1 规范的第 14.13 节详细介绍了内容-长度标头,并表示:
“SHOULD”一词在 RFC 中具有非常具体的含义:
因此,您可能并不总是看到内容长度。通常,您可能看不到任何动态生成的内容,因为这对于服务探索性 HEAD 请求来说可能太昂贵。例如,对 Apache 的静态文件的 HEAD 请求将具有内容长度,但对 PHP 脚本的请求可能没有。
例如,尝试这个网站...
那里没有内容长度。
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.
是的,
HEAD
响应的Content-Length
应该,但并非总是如此(请参阅 @Paul 的回答)包含GET 的
响应:Content-Length
值Stack Overflow 会:
Google 不会:
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:
与已接受的答案相反,RFC 7231 第 4.3.2 节状态:
——也就是说,Content-Length、Content-Range 、预告片和传输编码——
这比 甚至更弱 “https://stackoverflow.com/a/3854983/27358">保罗·迪克森的回答:
所以真正的答案是,您不需要包含 Content-Length,但如果包含,则应该给出正确的值。
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.
W3C 的 HTTP 规范 指出:
这(对我来说)意味着它应该保存“正确”的值,就像在
GET
响应。The HTTP-spec at W3C states:
Which (to me) means it should hold the "correct" value as you would in a
GET
response.根据 2022 年 6 月发布的最新 HTTP 规范 RFC 9110 - HTTP Semantics,
According to RFC 9110 - HTTP Semantics, the latest HTTP specification published in June 2022,