Is Content-Type mandatory in HTTP post request?

2019-02-16 08:10发布

Is the field:

Content-Type

mandatory when I perform an HTTP request?

标签: http post
2条回答
小情绪 Triste *
2楼-- · 2019-02-16 08:59

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

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".

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.

查看更多
Summer. ? 凉城
3楼-- · 2019-02-16 09:03

From the RFC2616

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".

So it is adviced to do, not mandatory. But the recipient might guess the content-type if none is given.

查看更多
登录 后发表回答