Friday, 13 March 2015

Post 33: What are HTTP Request Headers

A full HTTP request message is build on these blocks:

[method] [URL] [version] ←first section
[headers] ← middle section

[body] ← last section

The message is written in ASCII text. The first section always consists of the method, the URL, and the HTTP version (commonly it is HTTP 1.1 which exists since 1999. Recently HTTP 2 is released but still in beta state at time this post was created). The last section, the body section, can contain various data depending on the request, e. g. it can contain user log-in parameters or, if you want to upload file, a large amount of file data. The middle section contains at least the host HTTP header.

The header contains information that help server process the request, e. g. content negotiation. Let's say the client prefers to accept resources in French, then the header entry would contain the value “fr-FR” that would be assigned to the header “Accept-Language”. It would look like this:

Accept-Language: fr-FR

The specified value in the accept header doesn't garantuee that the client will receive the resource in the language he prefers though. For example if the server only offers a site in English, then the client will receive a site in English even though the client prefers French.

All of the headers must comply the HTML specification. Other commonly used headers are shown below:

Header Description
Referer Here the client sends the URL of the referring page to the server. This is usually used when the user clicks on a link and you want to know from which site the link was originally clicked on.
User-Agent This header contains information about the user agent (client, browser, software making the request). For example what browser, what version, etc.
Accept This header tells the server what the media types the browser prefers to accept. You usually need this for content negotiation.
Accept- Language This header describes the language the user agent prefers to accept.
If-Modified-Since Contains the date the user agent last downloaded and also cached the resource from the server. Knowing this information the server will only send the requested resource it was modified since that date.

The header listed above are just a sample of possible headers. One header can contain multiple values. An example of an HTTP request is shown below:

Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) Chrome/16.0.912.75
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

The multiple values in one header attribute describes the resources the client is willing to accept. For example the “Accept” header indicates that it accepts HTML, XHMTL+XML, and XML, but also everything else, i.e. “*/*”. The key “q” says the rate of preferability (aka. “quality value”, “relative degree of preference”). It is always between 0.0 to 1.0. The higher the number the number the higher the level of preferability. The default value is 1.0.

HTTP Succinctly by Scott Allen Syncfusion