据我理解,设定内容类型的地方有两处:
- 客户端为发送到服务器的正文(例如Post请求)设置内容类型。
- 服务器为响应设置内容类型。
这是否意味着在所有get请求(客户端部分)中都不需要或者不应该设置内容类型?如果可以或者应该设置,那么要设置什么样的内容类型呢?
此外,在几篇文章中我读到客户端的内容类型指定了客户端想要接收的内容类型。所以,也许我的第一点是错误的?
据我理解,设定内容类型的地方有两处:
这是否意味着在所有get请求(客户端部分)中都不需要或者不应该设置内容类型?如果可以或者应该设置,那么要设置什么样的内容类型呢?
此外,在几篇文章中我读到客户端的内容类型指定了客户端想要接收的内容类型。所以,也许我的第一点是错误的?
Content-Type
HTTP头字段仅应针对PUT
和POST
请求设置。GET请求不应该有Content-Type,因为它们不具有请求实体(即,请求主体)
Content-Type
是有意义的。 - patricknelsonContent-Type
存在,否则它拒绝回复 200。对我来说有些疯狂... - kevinarpeGET请求可以带有“Accept”标头,表明客户端理解哪些类型的内容。服务器可以根据此决定要发送哪种内容类型。
但是它们是可选的。
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
Accept
头部。然而,它们确实会关注 Content-Type
来确定请求者期望接收的格式。现在我意识到这就是为什么一些经常使用 GET 的 API 会使用查询字符串来传递格式参数的原因... - Gwyneth Llewelyn简短回答:大多数情况下,您不需要在HTTP GET请求中使用content-type头。但是规范似乎也没有排除HTTP GET请求中使用content-type头的可能性。
支持材料:
"Content-Type" is part of the representation (i.e. payload) metadata. Quoted from RFC 7231 section 3.1:
3.1. Representation Metadata
Representation header fields provide metadata about the representation. When a message includes a payload body, the representation header fields describe how to interpret the representation data enclosed in the payload body. ...
The following header fields convey representation metadata:
+-------------------+-----------------+ | Header Field Name | Defined in... | +-------------------+-----------------+ | Content-Type | Section 3.1.1.5 | | ... | ... |
Quoted from RFC 7231 section 3.1.1.5(by the way, the current chosen answer had a typo in the section number):
The "Content-Type" header field indicates the media type of the associated representation
In that sense, a Content-Type
header is not really about an HTTP GET request (or a POST or PUT request, for that matter). It is about the payload inside such a whatever request. So, if there will be no payload, there needs no Content-Type
. In practice, some implementation went ahead and made that understandable assumption. Quoted from Adam's comment:
"While ... the spec doesn't say you can't have Content-Type on a GET, .Net seems to enforce it in it's HttpClient. See this SO q&a."
However, strictly speaking, the specs itself does not rule out the possibility of HTTP GET contains a payload. Quoted from RFC 7231 section 4.3.1:
4.3.1 GET
...
A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.
So, if your HTTP GET happens to include a payload for whatever reason, a Content-Type
header is probably reasonable, too.
我发现这篇文章在澄清“Content-Type”概念方面非常有用,你可以查看一下,在文章末尾还列出了HTTP Content-type头的常见值列表。
curl
在设置任何Content-Type
头部时都没有问题,即使是GET请求,这样可以让API聪明地确定返回的格式。虽然这不是应该的方式——Accept
头部应该用于告诉服务器可接受的内容类型——但实际上很多聪明的API确实使用了它。请注意,这只是对规范的解释;规范并没有明确禁止这种机制,因此它被使用了。 - Gwyneth Llewelyn
GET
请求消息中的有效载荷没有定义的语义;在GET
请求中发送有效载荷主体可能会导致某些现有的实现拒绝该请求。”——我仍然解释为“不应该”(最佳实践),而不是明确的“必须不”,这只是建议您不应该期望服务器实现的一致性。但是,是的,如果您包含有效载荷,则“应该”还包括Content-Type
;只是GET
中的有效载荷不是标准的一部分。 - patricknelsonapplication/json
(而不是text/plain)。你可以发送一个Accept:
头部字段来指定这个 —— 这样或许更正确 —— 但并不是每个系统都会查看这个头部字段。人们只是假设如果请求需要XML,那么应该返回XML;同样,对于HTML、纯文本、JSON以及明天我们想出来的任何格式也是一样。 - Gwyneth Llewelyn