Format for 406 Not Acceptable payload?

2019-07-15 07:33发布

问题:

In a 406 Not Acceptable response:

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.

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

I can send a response like:

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

But then I am assuming a default Content-Type for the 406 payload (JSON in this case).

Or should I send a very simple, almost format-less, payload like:

application/json,application/pdf

回答1:

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

There's no standard for such payload.

You could choose any format that can be easily parsed by the user agent. In practice, both JSON or text should be fine:

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

See the following quote from the section 6.4.1 of the RFC 7231, which is referenced in the 406 status code definition:

[...] 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. [...]

MDN Web Docs from Mozilla suggests the 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. [...]