Is Content-Type mandatory in HTTP post request?

2019-02-16 08:36发布

问题:

Is the field:

Content-Type

mandatory when I perform an HTTP request?

回答1:

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.



回答2:

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.



标签: http post