消息正文为空时是否应该存在 Content-Type 标头? [英] Should Content-Type header be present when the message body is empty?
问题描述
Content-Type
标头是否应该在没有负载正文时出现在 HTTP 请求或响应中?
Should the Content-Type
header be present in an HTTP request or response when there is no payload body?
在这种情况下,HTTP 标头的正确组合是没有 Content-Type
且 Content-Length
为 0,或者应该是 Content-Type
消息没有正文时根本不存在?
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?
推荐答案
RFC 7231 说:
生成包含有效负载正文的消息的发送者应该在该消息中生成一个 Content-Type 标头字段 [...]
A sender that generates a message containing a payload body SHOULD generate a Content-Type header field in that message [...]
所以它没有明确说明如果您生成的消息 没有 有有效负载正文该怎么做,但它似乎暗示不发送 Content-Type 标头会在这种情况下是合适的.
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 是您向接收者指示不会有正文的方式,除非您使用诸如分块编码之类的东西.
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.
这篇关于消息正文为空时是否应该存在 Content-Type 标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!