HTTP header 到 return 服务器可用的内容类型

HTTP header to return server available content types

例如假设我们有一个 restful API 端点到 return 订单,它可以以不同的格式输出。

[GET] /orders/42 

可以 return xml、json 或 pdf。

我认为在 restful 范例中,最好的选择可能是 return 可用类型作为相对 OPTION 调用中的 header:

[OPTION] /tests

但我不知道是否存在类似内容协商服务器的东西 header 来列出可用的内容类型。

我想它应该看起来像:

Available-Content-Types: application/xml,application/json,application/pdf

是否存在类似的东西?

Content Negotiation 反过来。

客户端发出资源请求(通常使用 GET)并包括一个 Accept header 和一个可接受的 Content-Type 列表(使用质量值来描述偏好顺序)。

服务器然后根据此决定发送哪个响应。

所以客户可能会发送:

Accept: application/json;q=1, application/xml;q=0.9, application/pdf;0.5, */*;q-0.1

然后服务器将确定要return的数据类型。假设所有格式在服务器眼中都同样好,它会 return JSON 因为客户端的质量值最高。

Does something similar exists?

没有

Content Negotiation 指定为两种风格。

Proactive negotiation involves encoding the user agent's preferences into the initial request (using headers; for example: Accept)。服务器为响应选择合适的表示。

Reactive negotiation 更接近您要查找的内容 -- 用户代理根据服务器提供的信息选择资源。

300 Multiple Choices 向客户发出可供选择的表示形式的信号。

The 300 (Multiple Choices) status code indicates that the target resource has more than one representation, each with its own more specific identifier, and information about the alternatives is being provided so that the user (or user agent) can select a preferred representation by redirecting its request to one or more of those identifiers.

Location header 用于标识哪个备选方案是服务器首选参考,但没有用于枚举其他备选方案的标准。

For request methods other than HEAD, the server SHOULD generate a payload in the 300 response containing a list of representation metadata and URI reference(s) from which the user or user agent can choose the one most preferred. The user agent MAY make a selection from that list automatically if it understands the provided media type.

在网络上,这可能看起来像一个网页,其中包含编码到其中的链接列表。浏览器会呈现列表,用户可以选择任何合适的。服务器的首选选择将 在位置 header 中描述,并且用户代理可以简单地 re-direct 到首选的替代方案,而无需先咨询用户。

如果有 header 可以完成这项工作,您会在 IANA header registry 中找到它;我没有看到任何看起来匹配的东西,所以它是 media-type 或什么都没有。