HTTP 上的多个值 headers

Multiple values on HTTP headers

这是正确的吗?

var headers = new Headers();
headers.append('Accept', 'application/json, application/pdf')

我想同时接受 json 和 pdf 文件。

是的,it is correct。示例表单 w3c:

Accept: text/plain, text/html

更一般地说,多个值 headers 必须可以表示为 comma-separated 列表,因此以这种方式表示值是有效的。包含多个 Accept headers 也是有效的,服务器可以选择将其压缩回 comma-separated 列表。

来自 HTTP RFC:https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html

Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded.