reggie
reggie

Reputation: 3674

What is the correct response format of a 406 HTTP status error

I want to issue a 406 Not Acceptable error in my application and I want to alert the client about the available alternative formats.

From the HTTP protocol spec:

Unless it was a HEAD request, the response SHOULD include an entity containing a list of available entity characteristics and location(s) from which the user or user agent can choose the one most appropriate. The entity format is specified by the media type given in the Content-Type header field. Depending upon the format and the capabilities of the user agent, selection of the most appropriate choice MAY be performed automatically. However, this specification does not define any standard for such automatic selection.

Do I add that entity in the response body? What's the format of that list?

Upvotes: 1

Views: 425

Answers (1)

user3417917
user3417917

Reputation: 196

The main thing in the spec is:

The entity format is specified by the media type given in the Content-Type header field

This page is quite useful: http://chimera.labs.oreilly.com/books/1234000001708/apc.html#_proactive_negotiation

As-is the article it references: http://bit.ly/agent-conneg

The examples there describe negotiation, in situations where you would return a 300 Multiple Choices response, a situation similar to how you might provide alternatives in a 406 Not Acceptable response. It follows the same ideas where you provide an entity in the format of the returning content type - if you are returning text, write some text; if you are returning HTML, write some HTML.

HTTP/1.1 300 Multiple Choices
Host: www.example.org
Content-Type: application/xhtml
Content-Length:XXX

<p>
  Select one:
</p>
<a href="/results/fr" hreflang="fr">French</a>
<a href="/results/en-US" hreflang="en-US">US English</a>
<a href="/results/de" hreflang="de">German</a>

A standard format would indeed be useful for automatic renegotiation, but the spec stays away from defining that. I would agree with the writer that the best way to inform of alternatives to a 406 is to use the same "Link" headers in the example from the oreilly.com page, as they describe:

An alternative approach is to use Link headers. This has the advantage of being a standard header that any client can understand. Here is an example:

HTTP/1.1 300 Multiple Choices
Host: www.example.org
Content-Length: 0
Link: <http://www.example.org/results/png>; type="image/png",
      <http://www.example.org/results/jpeg>;type="image/jpeg",
      <http://www.example.org/results/gif>;type="image/gif"

However, don't expect guaranteed results on every client with something that is not standardized.

Upvotes: 1

Related Questions