406 Not Acceptable 负载的格式?

Format for 406 Not Acceptable payload?

在 406 不可接受的响应中:

The server SHOULD generate a payload containing a list of available representation characteristics and corresponding resource identifiers from which the user or user agent can choose the one most appropriate. A user agent MAY automatically select the most appropriate choice from that list. However, this specification does not define any standard for such automatic selection, as described in RFC7231 Section 6.4.1.

可用表示特征列表和相应的资源标识符”是否有首选格式?

我可以发送这样的回复:

{ Acceptable: ["application/json", "application/pdf"] }

但是我假设 406 负载的默认内容类型(在本例中为 JSON)。

或者我应该发送一个非常简单的,几乎 无格式,有效载荷,如:

application/json,application/pdf

Is there a preferred format for that "list of available representation characteristics and corresponding resource identifiers"?

此类负载没有标准。

您可以选择用户代理可以轻松解析的任何格式。实际上,JSON 或 text 都应该没问题:

{ "acceptable" : [ "application/json", "application/pdf" ] }
application/json,application/pdf

请参阅 6.4.1 of the RFC 7231, which is referenced in the 406 状态代码定义部分的以下引用:

[...] A specific format for automatic selection is not defined by this specification because HTTP tries to remain orthogonal to the definition of its payloads. In practice, the representation is provided in some easily parsed format believed to be acceptable to the user agent, as determined by shared design or content negotiation, or in some commonly accepted hypertext format. [...]

Mozilla 的 MDN Web 文档建议 following:

[...] In reality, this error is very rarely used: instead of responding using this error code, which would be cryptic for the end user and difficult to fix, servers ignore the relevant header and serve an actual page to the user. It is assumed that even if the user won't be completely happy, they will prefer this to an error code. [...]