Quality Values
May 20, 2023
Quality values, also known as q-values or q-factors, are parameters used in HTTP headers to indicate the relative preference of a particular response or content type. They are used to specify the degree to which a user agent, such as a web browser, prefers one type of content over another when multiple options are available. Quality values are expressed as decimal numbers between 0 and 1, with 1 being the highest preference, and 0 being the lowest preference.
Purpose and Usage
HTTP headers are pieces of information that are sent between a client and a server during communication over the internet. The headers contain important information about the request and response, such as the content type, language, encoding, and caching directives. Quality values are used in HTTP headers to provide additional information about the preferences of the user agent making the request.
For example, when a web browser requests content from a server, it may specify that it prefers to receive HTML content rather than XML content. This preference is communicated to the server through the “Accept” header, which contains a list of content types that the browser can handle, along with quality values that indicate the degree of preference for each type. The server can then use this information to determine which type of content to send back to the browser.
Here is an example of an “Accept” header with quality values:
Accept: text/html;q=0.9, application/xhtml+xml;q=0.8, application/xml;q=0.7, */*;q=0.5
In this example, the browser is indicating that it prefers HTML content over XHTML content, which it prefers over XML content. The quality values range from 0.9 for HTML to 0.5 for any other type of content. The “*” wildcard character is used to indicate that the browser will accept any other content type with a quality value of 0.5.
Quality values can also be used in other HTTP headers, such as “Accept-Encoding” to indicate the preferred encoding type, and “Accept-Language” to indicate the preferred language.
Syntax and Rules
Quality values are expressed as decimal numbers between 0 and 1, with a maximum of three decimal places. The default value is 1 if no quality value is specified. If multiple types have the same quality value, they are considered equally preferred. If a type has a quality value of 0, it means that the user agent does not accept that type of content.
Here are some examples of valid quality values:
- 1
- 0.9
- 0.5
- 0.001
Here are some examples of invalid quality values:
- 1.234 (more than three decimal places)
- -0.5 (negative value)
- 2 (value greater than 1)
Quality values can be combined with content type parameters to provide additional information about the content being requested. For example, the following “Accept” header indicates that the browser prefers JSON content with a UTF-8 encoding:
Accept: application/json;q=0.9;charset=utf-8
Negotiation and Content Selection
When a user agent sends an HTTP request to a server, it includes one or more “Accept” headers that specify the types of content it can handle, along with quality values that indicate the degree of preference for each type. The server responds with a content type that best matches the preferences of the user agent.
The process of selecting the appropriate content type is known as content negotiation. There are two types of content negotiation: server-driven and agent-driven.
Server-driven negotiation occurs when the server examines the “Accept” header of the request and selects the most appropriate content type based on the preferences specified. The server may also take into account other factors, such as the availability of the requested content and the capabilities of the user agent.
Agent-driven negotiation occurs when the user agent sends a request for a specific content type, and the server responds with that type regardless of the preferences specified in the “Accept” header. This type of negotiation is less common than server-driven negotiation.