文章来源:https://tools.ietf.org/html/rfc7231#section-1.1
例子:
Accept:text/html,application/xhtm +xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:zh-CN,zh;q=0.8
Connection:keep-alive
q=0.9: q:表示优先权,先接受那个类型。
这个Accept 头属性能被用在浏览器响应能接受的media 类型。
The "Accept" header field can be used by user agents to specify response media types that are acceptable.
这个“Accept” 头属性能被浏览器用来指定响应的media 类型,表示自己可以接受的类型。
Accept header fields can be used to indicate that the request is specifically limited to a small set of desired types,
as in the case of a request for an in-line image.
Accept = #( media-range [ accept-params ])
media-range = ( "*/*":所有类型
/ ( type "/""*" ) :type 类型子类型
/ ( type "/"subtype ) type/type的子类型
) *( OWS ";"OWS parameter )
accept-params = weight *( accept-ext )
accept-ext = OWS ";" OWS token ["=" ( token / quoted-string ) ]
The asterisk "*" character is used to group media types into ranges,
with "*/*" indicating all media types and "type/*" indicating all subtypes of that type.
The media-range can include media type parameters that are applicable to thatrange.
Each media-range might be followed by zeroor more applicable media type parameters (e.g., charset), an optional"q" parameter for indicating a relative weight (Section 5.3.1), and then zero or more
extension parameters.
The "q" parameter is necessary if any extensions (accept-ext) are present, since it acts as a separator
between the two parameter sets.
q 参数是必要的,如果一些扩展是出现了,因为他在两个参数集里面起作用。
Note: Use of the "q" parameter name to separate media type parameters from Accept extension parameters is due to historical practice.
注:使用q参数把media 类型参数和可接受的扩展参数相分离是由于历史的实践。
Although this prevents any media type parameter named "q" from being used with a media range,
尽管阻止命名q的一些media 类型参数?不在一个media 范围里使用。命名q的一些media 类型参数:不知道是哪些media 类型命名了?
such an event is believed to be unlikely given the lack of any"q" parameters in the IANA media type registry and the rare usage of any media type parameters in Accept.
Future media types are discouraged from registering any parameter named"q".
The example
Accept: audio/*; q=0.2, audio/basic
is interpreted as "I prefer audio/basic, but send me any audio type:
我更喜欢音频/basic,但却给我任何音频类型.
if it is the best available after an 80% mark down in quality.在质量上下降80%后,如果它是最好的可利用的???
A request without any Accept header field implies that the user agent will accept any media type in response.
一个请求没有任何的Accept 头属性暗示浏览器可以接受任何类型回应。
If the header field is present in a request and none of the available representations for the response have a media type that is listed as acceptable,
如果头属性出现在一个request中,但是tomcat 没有这样类型回应,将送一个406的回应或是忽略头属性,好像它不受内容的约束。
the origin server can either honor the header field by sending a 406 (Not Acceptable) response or disregard the header field by treating the response as if it is not subject to content negotiation.
A more elaborate example is
Accept: text/plain; q=0.5, text/html,
text/x-dvi; q=0.8, text/x-c
Verbally, this would be interpreted as"text/html and text/x-c are the equally preferred media types, but if they do not exist, then
口头上,这个将被解释作为 text/html and text/x-c 是相等更好的media 类型,但是如果他们不存在,送一个 text/x-dvi 表示,如果这个不存在,送一个text/plain 表示
send the text/x-dvi representation, and if that does not exist, send the text/plain representation".
Media ranges can be overridden by more specific media ranges or specific media types.
Media 范围能被更具体的media 范围 或具体的media 类型重写。
If more than one media range applies to a given type, the most specific reference has precedence.
如果 多个media 范围应用到一个给定的类型, 最具体的引用具有优先权。
For example,
Accept: text/*, text/plain,text/plain;format=flowed, */*
有下面的优先级:
1. text/plain;format=flowed
2. text/plain
3. text/*
4. */*
The media type quality factor associated with a given type is determined by finding the media range with the highest precedence
that matches the type.
media type的quality值和一个给定类型相关联,这个类型是首先查找一个media 的范围,取最高优先权的类型。
For example,
Accept: text/*;q=0.3, text/html;q=0.7,text/html;level=1,text/html;level=2;q=0.4, */*;q=0.5
关联如下:
+-------------------+---------------+
| Media Type | Quality Value |
+-------------------+---------------+
| text/html;level=1 | 1 |
| text/html | 0.7 |
| text/plain | 0.3 |
| image/jpeg | 0.5 |
| text/html;level=2 | 0.4 |
| text/html;level=3 | 0.7 |
+-------------------+---------------+
Note: A user agent might be provided with a default set of quality values for certain media ranges.
However, unless the user agent is a closed system that cannot interact with other rendering agents, this
default set ought to be configurable by the user.