HTTP 协议是否支持响应标头中的多种内容类型?
我想知道我是否可以返回响应标头
Content-Type: application/json text/json
,或者这不合法吗?
HTTP 协议规定 Content-Type 应该是 mediaType 并且 mediaType 定义如下:
media-type = type "/" subtype *( ";" parameter )
type = token
subtype = token
但我无法解释这意味着什么,有人可以启发我吗?
谢谢!
I'm wondering if I can return the response header
Content-Type: application/json text/json
or isn't this legal?
The HTTP protocol states that Content-Type should be of mediaType and that mediaType is defined as the following:
media-type = type "/" subtype *( ";" parameter )
type = token
subtype = token
But I'm having trouble interpreting what this means, can anyone enlighten me?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要查看标头字段的定义:
http://greenbytes .de/tech/webdav/rfc2616.html#rfc.section.14.17
Content-Type = "Content-Type" ":" 媒体类型,
因此需要单一媒体类型,由您上面引用的语法定义。
所以答案是:单个类型/子类型,后跟可选参数。
You need to look at the definition of the header field:
http://greenbytes.de/tech/webdav/rfc2616.html#rfc.section.14.17
Content-Type = "Content-Type" ":" media-type
so it takes a single media-type, which is defined by the grammar you quoted above.
So the answer is: a single type/subtype, followed by optional parameters.
不可以。正如接受的答案中所指出的,标头值本身不允许有多个值,也不允许发送多个 Content-Type 标头:
https://www.rfc-editor.org/rfc/rfc7230#section -3.2.2
没有一个“转义子句”成立,因为
media-type
不允许使用逗号分隔的值列表,并且Content-Type
显然也不是一个众所周知的例外。No. As pointed out in the accepted answer, the header value itself does not allow for multiple values, and it is also not allowed to send multiple Content-Type headers:
https://www.rfc-editor.org/rfc/rfc7230#section-3.2.2
None of the "escape clauses" holds, because
media-type
does not allow a comma-separated list of values, andContent-Type
evidently is not a well-known exception, either.