406 HTTP 状态错误的正确响应格式是什么

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

我想在我的应用程序中发出 406 不可接受错误,我想提醒客户可用的替代格式。

来自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.

我是否将该实体添加到响应正文中? 该列表的格式是什么?

规范中的主要内容是:

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

这个页面很有用: http://chimera.labs.oreilly.com/books/1234000001708/apc.html#_proactive_negotiation

As-is 引用的文章: http://bit.ly/agent-conneg

那里的示例描述了谈判,在您 return 300 多项选择响应的情况下,这种情况类似于您可能如何在 406 不可接受的响应中提供备选方案。它遵循与您提供 returning 内容类型格式的实体相同的想法 - 如果您是 returning 文本,请写一些文本;如果你 returning HTML,写一些 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>

标准格式确实对自动重新协商很有用,但规范并未对其进行定义。我同意作者的观点,告知 406 替代方案的最佳方法是在 oreilly.com 页面的示例中使用相同的 "Link" headers,正如他们所描述的那样:

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"

但是,不要指望使用非标准化的东西在每个客户身上都能得到保证的结果。