Format for 406 Not Acceptable payload?

为君一笑 提交于 2019-12-24 00:53:50

问题


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



来源:https://stackoverflow.com/questions/50102277/format-for-406-not-acceptable-payload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!