What is the purpose of the “q” values in the HTTP

2020-02-19 04:00发布

问题:

I have made a http request using Firefox.Now the request header shows the following:

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

But curious to know what is q=0.9,*/*;q=0.8

回答1:

Each media-range MAY be followed by one or more accept-params, beginning with the "q" parameter for indicating a relative quality factor. The first "q" parameter (if any) separates the media-range parameter(s) from the accept-params. Quality factors allow the user or user agent to indicate the relative degree of preference for that media-range, using the qvalue scale from 0 to 1. The default value is q=1

The information is available here

A nice explanation can be found here as well.



回答2:

The Accept header list is first split at , then at ; for additional parameters per entry. So, the list in your example splits down to text/html, application/xhtml+xml, application/xml;q=0.9 and */*;q=0.8. The q= parameter on each entry indicates to the server the degree of preference for that media-type. It defaults to its maximum value of 1, if it is missing (like in the first 2 entries). The last entry of */*;q=0.8 indicates to the server, that eventually any content-type would be acceptable but would be less preferable than the others listed. Otherwise, the server might decide to send no content at all, because the client would not "accept" it anyway.



回答3:

To add to @Robin479's correct answer, the accept header in the question is equivalent to,

Accept:
text/html;q=1,
application/xhtml+xml;q=1,
application/xml;q=0.9,
*/*;q=0.8

NOTE: The new line characters are just for better readability.



回答4:

The Q value is aways a number from zero to one and it represents the relative quality value. The default Q value is 1.0.

So something with Q value 0.9 is just slightly more preferred than something with Q value of 0.6.