q=
is the "weight" factor, ie it is used to indicate the priority for the server (backend), it is important to note that this can be used in other headers besides Accept
as per example:
-
Accept-Charset
-
Accept-Language
-
Accept-Encoding
-
TE
The
q=
can be called Quality values or q-values or q-factors and are used to describe the order of priority of values in a comma-separated list. The importance of a value is marked by the suffix
; q =
immediately followed by a value between 0 and 1 included (0.0 to 1.0 for example), with up to three decimal digits, the highest value denoting the highest priority and when it is not the default value is 1 . As described in
link
Example:
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
It will indicate the following order of priority:
+===================================+============+
| Valor | Prioridade |
+===================================+============+
| text/html e application/xhtml+xml | 1.0 |
+-----------------------------------+------------+
| application/xml | 0.9 |
+-----------------------------------+------------+
| */* | 0.8 |
+-----------------------------------+------------+
Then this Accept
indicates what type of content-type the browser accepts and q=
will set the priority, in the above case the wait request states for the bakc-end that accepts text/html
, application/xhtml+xml
and application/xml
, but prioritize the first 2, if available.
Note that */*
probably states that if you do not have the first 3 in the order of priority, then any type will be accepted, of course it is possible to customize at the time of the request, an example with XMLHttpRequest
(ajax) :
var oReq = new XMLHttpRequest();
//Defina como false
oReq.open("GET", "/url", true);
oReq.setRequestHeader('Accept', 'application/json');
...
//Espera completar a requisição, geralmente congela o browser
oReq.send(null);
In this example, as I did not specify q=
, then it would be equivalent to saying q=1
to application/json
, however this has to be resolved on the back end (read the end where I wrote extra ).
Accept-Language
An interesting example of usage I think for most is with Accept-Language
, because if the browser or a request define something like:
Accept-Language: pt, en-gb;q=0.8, en;q=0.7
You can resolve on the server side to search for pt
, if you do not find the priority would be British English and if it is not yet available this language for the specified page will display the > English ( en;q=0.7
).
Extra
In PHP there is a Locale::acceptFromHttp
function, but I think it's only possible to install via PECL or manually
Here in this question SOen has examples in PHP and C # of how to get the priority language:
In Java you probably did not create an example because for Web in Java there are different technologies.