HTTP status code for unaccepted Content-Type in request

39,588

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

Share:
39,588

Related videos on Youtube

Jordan
Author by

Jordan

Updated on May 07, 2020

Comments

  • Jordan
    Jordan almost 4 years

    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?

    • Chris Halcrow
      Chris Halcrow over 10 years
      See here for a definitive answer: stackoverflow.com/questions/19417553/…
    • Frans
      Frans about 2 years
      Your server couldn't technically parse the content if the Content-Type was incorrect: how would it know for sure how to parse it?
  • blackstrype
    blackstrype over 9 years
    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.
  • Julian Reschke
    Julian Reschke almost 9 years
    415 is correct, but you shouldn't use RFC 2616; it has been obsoleted by RFC 7231.
  • Ashith
    Ashith about 8 years
    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).
  • user192127
    user192127 almost 8 years
    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
  • Frans
    Frans about 2 years
    Content without a content type is basically meaningless. So the distinction that @blackstrype and @user192127 seem to want to make between the two doesn't exist. If you get a Content-Type header that indicates a media type you don't accept, 415 Unsupported Media Type is absolutely the correct response. If you get content with no Content-Type header, then you should reject the request outright because you would have no definitive way to know how to parse the content.