Sunday, May 1, 2011

How to interpret HTTP Accept headers ?

According to the HTTP1.1 spec, an Accept header of the following

Accept: text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c

is interpreted to mean

text/html and text/x-c are the preferred media types, but if they do not 
exist, then send the text/x-dvi entity, and if that does not exist, send 
the text/plain entity

Let's change the header to:

Accept: text/html, text/x-c

What is returned if neither of this is accepted ? e.g. let's pretend that I only support application/json

From stackoverflow
  • Maybe you should respond with a 406 Not Acceptable. That's how I read this.

    Or a 415 Unsupported Media Type?

    I would opt for a 406, because in that case and according to the specs, a response SHOULD include a list of alternatives. Although is not clear to me how that list should look like.

    8jean : 406 is correct in this case. 415 is for the case where a Client submits data (e.g. via POST) with a Content-Type the application can't handle.
  • "If an Accept header field is present, and if the server cannot send a response which is acceptable according to the combined Accept field value, then the server SHOULD send a 406 (not acceptable) response." -- RFC2616, Section 14.1

  • You have a choice. You can either reply with 406 and include an "entity" (e.g. HTML or text file) describing the available formats; OR if you are using HTTP 1.1, you can send the format you support even though it wasn't listed in the Accept header.

    (see section 10.4.7 of RFC 2616)

    "Note: HTTP/1.1 servers are allowed to return responses which are not acceptable according to the accept headers sent in the request. In some cases, this may even be preferable to sending a 406 response. User agents are encouraged to inspect the headers of an incoming response to determine if it is acceptable."

0 comments:

Post a Comment