31

Is the field:

Content-Type

mandatory when I perform an HTTP request?

user601836
  • 3,215
  • 4
  • 38
  • 48
  • Consider that if you don't send one, the other side will have no idea how to interpret the payload. – Oded Apr 07 '13 at 09:14

2 Answers2

39

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.

bdash
  • 18,110
  • 1
  • 59
  • 91
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.

Ritch Melton
  • 11,498
  • 4
  • 41
  • 54
rene
  • 41,474
  • 78
  • 114
  • 152
  • "But the client..." You mean "but the server..." else it makes no sense ;-) – Pascal Jan 12 '14 at 21:52
  • I doubt it [The Content-Type entity-header field indicates the media type of the entity-body sent to the recipient](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17) but I'll use recipient to avoid confusion. – rene Jan 12 '14 at 22:05