47

For certain resources, my RESTful server only accepts PUT and POST requests with JSON objects as the content body, thus requiring a Content-Type of application/json instead of application/x-www-form-urlencoded or multipart/form-data or anything else.

Malformed JSON (or lack thereof) returns a 400 with the error message taken directly from the exception raised by the JSON parser, for debugging purposes.

Which HTTP error code means that the client sent a request with an unacceptable Content-Type, even if the server could technically parse the request content?

Jordan
  • 4,120
  • 4
  • 29
  • 40
  • 1
    See here for a definitive answer: http://stackoverflow.com/questions/19417553/what-if-any-http-status-code-is-returned-when-a-mime-type-is-missing – Chris Halcrow Oct 23 '13 at 03:33

1 Answers1

67

It could be 415 Unsupported Media Type according to this list: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16.

Pylinux
  • 9,386
  • 3
  • 52
  • 62
William Durand
  • 5,199
  • 1
  • 24
  • 37
  • 1
    I'm still wondering if there a difference between "unnaccepted content type" and "unsupported media type" -- where the actual content (potentially different from the declared content) does not match with what is intended. – blackstrype Nov 17 '14 at 17:55
  • 2
    415 is correct, but you shouldn't use RFC 2616; it has been obsoleted by RFC 7231. – Julian Reschke May 06 '15 at 16:25
  • 23
    `415 Unsupported Media Type` means the client has *provided* data in a format that the server doesn't support (as indicated by the request's `Content-Type` header). For example, trying to do a `POST` request to create record of type `User` with the resource in format `application/xml` but the server can't process XML requests for that resource type. `406 Not Acceptable` means the incoming request is wanting the response data in a specific format (as indicated by the `Accept` header) which the server can't provide (for example, wanting a record as XML when the server only provides JSON). – Anthony Apr 24 '16 at 23:03
  • 1
    careful parsing of the language of 7231 (2616 is superseded) says otherwise "The 415 (Unsupported Media Type) status code indicates that the origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource. The format problem might be due to the request's indicated Content-Type or Content-Encoding, or as a result of inspecting the data directly." - read that carefully. 415 is widely regarded (not correctly IMO) as appropriate but it is not. Convenient use is 1 thing; provable correctness is another – user192127 Jul 22 '16 at 13:39
  • @user192127: in that quote the word "payload" does not explicitly specify whether the request or response payload is meant, but with the noun "the request" immediately preceding it, grammatically, that it means the request payload is the most reasonable interpretation. – EricS Jun 27 '17 at 18:46
  • @user192127 I would also add that the server method would not create the response payload if it is refusing to service the request. So if it meant the response payload it should not refer to it as if exists, but rather use wording like "the payload would be in a format not supported..." – EricS Jun 27 '17 at 18:53
  • 3
    See also https://stackoverflow.com/questions/3294483/http-406-and-415-error-codes where the most upvoted answer contradicts this answer. – EricS Jun 27 '17 at 18:58