且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HTTP post请求中是否必须使用Content-Type?

更新时间:2022-06-05 07:16:18

不,这不是强制性的。根据 HTTP 1.1规范

No, it's not mandatory. Per the HTTP 1.1 specification:


包含实体主体的任何HTTP / 1.1消息应该包括定义该主体的媒体类型的Content-Type头字段。当且仅当媒体类型不是由Content-Type字段给出时,接收者可以尝试通过检查其内容和/或用于标识资源的URI的名称扩展来猜测媒体类型。如果媒体类型仍然未知,收件人应该将其视为application / octet-stream类型。

Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body. If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource. If the media type remains unknown, the recipient SHOULD treat it as type "application/octet-stream".

这就是说,显然如果省略Content-Type标题,服务器很难准确地解释您发送的数据,因此不建议这样做。

That said, it's obviously going to be hard for the server to accurately interpret the data you're sending if you omit the Content-Type header, so it's not recommended to do so.