32

Is the field:

Content-Type

mandatory when I perform an HTTP request?

2
  • Consider that if you don't send one, the other side will have no idea how to interpret the payload.
    – Oded
    Commented Apr 7, 2013 at 9:14
  • Uhm, no. The default is "application/octet-stream", which makes sense. By the end of the day, the payload is nothing but bytes and will always be bytes. The presence of a content type provides an alternative method how those bytes can be decoded into a different representation. Commented Oct 8, 2023 at 9:00

2 Answers 2

40

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.

10

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.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.