220
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

What do these q=%f mean?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
kolypto
  • 31,774
  • 17
  • 105
  • 99

3 Answers3

285

This is called a relative quality factor. It specifies what language the user would prefer, on a scale of 0 to 1, as can be seen from the HTTP/1.1 Specification, §14.4:

Each language-range MAY be given an associated quality value which represents an estimate of the user's preference for the languages specified by that range. The quality value defaults to "q=1". For example,

  Accept-Language: da, en-gb;q=0.8, en;q=0.7

would mean: "I prefer Danish, but will accept British English and other types of English."

Community
  • 1
  • 1
  • Accept-Charset has q-values in the example as well. :) – bzlm Dec 18 '11 at 16:10
  • 44
    Very nice, thanks! But why not just list them in the order of preference? – kolypto Dec 18 '11 at 16:12
  • 2
    @o_OTync: It's difficult to indicate explicit anti-preference with such a system. – Lightness Races in Orbit Dec 18 '11 at 16:14
  • 12
    So what *exactly* does it mean to accept 80% qualitied en-gb? – Pacerier Jan 18 '12 at 07:08
  • 3
    @Pacerier: It means that, in the case that your website is multilingual, the user would prefer the content to be served in British English, rather than American English. –  Jan 18 '12 at 13:18
  • 9
    @TimCooper Why not just have the languages in order or preference? The value doesn't seem to matter, other than it being larger than others. – Rob May 18 '16 at 00:29
  • @Rob Because it *could* matter---you could write a browser/app/etc. that makes decisions based on the score, and as a previous user mentioned, you can't specify anti-preference with an ordered list. – EntangledLoops Jul 06 '16 at 16:39
  • 7
    to @kolypto, parameter lists, whether in a URL or in a header doen't have an implicit order. – JP Silvashy Jan 24 '17 at 19:58
  • @EntangledLoops, How do you specify anti-preference? Negative values? – Pacerier Jun 21 '17 at 00:24
  • 2
    @Pacerier Ideally, except the scale is 0-1. On this scale, I would argue that a preference of "0" is a stronger indicator of "do not want" then leaving the value absent altogether. – EntangledLoops Jun 21 '17 at 15:01
  • 4
    @kolypto I think the reason is for more flexibility (your client script can just append desired types, instead of making sure entire list is always sorted). But even more fascinating reason is that it makes your client allow the server to decide which type is better! Look: if the standard was just an ordered list, you could say that for example you prefer HTML over PDF. But what if you are totally fine with both equally? And my website sends awesome PDFs and just good HTML. If you are indifferent, I prefer to send a PDF for you to make you happier! – Robo Robok Nov 11 '17 at 11:00
  • 1
    @Pacerier According to [RCF2616 section 3.9](https://tools.ietf.org/html/rfc2616#section-3.9): `"If a parameter has a quality value of 0, then content with this parameter is 'not acceptable' for the client."` – Overleaf Dec 15 '17 at 13:37
  • 2
    The reason order is less expressive @kolypto is because order alone doesn’t permit indicating _equal_ preference. Having ruled out any more preferred options, facing two or more equally preferred options which are supported, the server may then apply _its_ preference to choose between them. That’s why it’s called "negotiation" — "of the types I support, which do you most prefer? oh, these two are equally acceptable? okay, well I prefer to send this one." Language is actually an odd case where by convention, order _does_ indicate preference (and this is called out in spec) on top of qvalue. – Semicolon Apr 28 '18 at 17:33
  • is the most preferred language always the first parameter in the header? – oldboy Jul 31 '19 at 04:29
  • @oldboy: the specification says "The quality value defaults to "q=1". My interpretation is that it does not matter which parameter you put first (except maybe when they have equal value). In the example `Accept-Language: da, en-gb;q=0.8, en;q=0.7` the weight for `da` would be equaln in *either* of these examples `Accept-Language: en-gb;q=0.8, en;q=0.7, da=1` or `Accept-Language: en-gb;q=0.8, da, en;q=0.7` – mortb Jun 11 '21 at 11:42
  • If two languages have same weight the order may matter as in `Accept-Language: da, en-gb` when (according to @Semicolon above) the server may use the order or its *own* preference (that is the server's preference) – mortb Jun 11 '21 at 11:48
  • I don't know of any OS/browser combination that would support a q=0 – PRMan Jul 09 '22 at 21:25
41

It is called the relative quality factor in the specification:

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 (section 3.9). The default value is q=1.

...

The example

Accept: audio/*; q=0.2, audio/basic

SHOULD be interpreted as "I prefer audio/basic, but send me any audio type if it is the best available after an 80% mark-down in quality."

See also:

HTTP/1.1: Header Field Definitions

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
  • 2
    I'm really late to the party - but what do you mean "after an 80% mark-down in quality"? It's the degree of preference, not the quality of the audio file, right? Or am I misreading this? – Mave Apr 21 '15 at 14:08
  • 11
    @Mave: Well, in practice it's probably mostly used as a measure of preference. However, in its original formulation, I do believe it's meant to be based on the quality of the encoding. Consider, for instance, FLAC vs MP3. You could say something like: "Send me the MP3 version if it at worst is 50% of the quality of the lossless version. If it isn't, send me the FLAC version." How to quantify such quality, and whether that is used in practice, I do not know, but I do believe that to be the original intention. – Sebastian Paaske Tørholm Apr 21 '15 at 14:47
  • 1
    @SebastianPaaskeTørholm I came here looking for more info on this. There are only two places in RFC 7231 that seem to hint at this secondary meaning, but it’s not very clear to me how it was intended to work, since the two meanings could conflict with each other — for example it seems like it would be impossible to express a preference for a lossier type over a less lossy type if qvalue is overloaded this way. – Semicolon Apr 28 '18 at 17:38
  • is the most preferred language always the first parameter in the header? – oldboy Jul 31 '19 at 04:29
  • @BugWhisperer The linked standard has a more elaborate example, where it isn't first. Please consult that. :) – Sebastian Paaske Tørholm Aug 02 '19 at 11:21
14

Accept-languages quality parameter can be understood like this:

by attaching Accept-Language: pl_PL;q=0.8, en_US;q=0.2, header to the request we communicate to the server following message

Server, please provide me Polish translation of this website, but please do that only if its quality is >= %80% of related-quality-factor. In case quality is <80% - I do not mind getting the English version, because it is irrelevant for me since I speak English anyway.

So for example, as a web page content provider we might introduce Accept-Language header parsing in such a way, that our website is suitable for foreigners speaking no English at all (then even translated navigation would be helpful (pages having translated only the navigation, lets say, have q=0.1), where fully translated content has q=0.9 and fully translated content and verified by native speakers speaking both original language and translated language content might have q=0.99 - because this is the only possible situation when meaning of the content is fully transduced)

test30
  • 3,496
  • 34
  • 26
  • is the most preferred language always the first parameter in the header? – oldboy Jul 31 '19 at 04:29
  • @BugWhisperer yes, but if you want to be really strict to the standard and if your translation is pretty simple or weak - you might want to skip the first value – test30 Aug 08 '19 at 15:23
  • why would using the second value be more strict? – oldboy Aug 08 '19 at 23:41