In the second part of our short http course we will look at http headers.
If you’ve missed part 1 then here it is- Part 1- http basics
HTTP headers re used to convey additional information between the client and the server.
Although they are optional they make up the most of the http request and are almost always present.
When you request a web page using a web browser the headers are inserted automatically by the web browser, and you don’t see them.
Similarly the response headers are inserted by the web server and are not seen by the user.
There are extensions available for both Firefox and Chrome that let you view http headers and also command line tools like curl.
We will look at some example request and response headers later.
Request and Response Header Structure
Request and response headers share a common structure.
They consist of a header name + colon + header value. Example
An header can have multiple values, in which case they are separated using a comma.
Field names are case insensitive according to the RFC 2616 but field values should be treated as case sensitive
There are many headers, and it is not important to be familiar with them all.
There is a really good list of headers with explanation on tutorialspoint
You should note that only necessary headers are sent all other headers are assumed by the web server and client to be their default.
For example the connection header is not normally sent as the default behaviour is keep-alive and this is assumed by the server.
Common Request Headers
The original HTTP protocol used non persistent connections.
This meant that the client
- Made a request
- Got a Response
- Closed the Connection
If you consider a TCP/IP connection to be the same as a telephone connection. This means that you:
- Dial the number and get an answer (connection established.
- Say something and get an acknowledgement
- Hang Up.
Because it takes time and resources to establish the connection in the first place it makes no sense to drop it so quickly.
Therefore in HTTP 1.0 the client can tell the server that it will keep the connection open by using the connection: keep-alive header.
In HTTP v1.1 the default behaviour was changed and persistent connections become the default mode.
Now the client can tell the server that it will close the connection by using the header connection: close.
Almost all websites,including this one, use shared hosting.
With shared hosting the web server is configured as a virtual host and all virtual hosts will be assigned to a single IP address.
The hosts header tells the web server which server to refer the request to e.g.
This gives information about the client making the request as shown below:
Accept Request Headers
These headers are used for content negotiation and are sent by the client (browser) to the server, and tells the server what formats the client can understand.
The Accept header is used to tell the server what media types the client prefers e.g. Text, audio etc.
For normal web pages common values are text/plain and text/html.
For JSON encoded data the header
Other accept headers are
Example Headers from live session:
Common Response Headers
The screen shot below shows the response headers using the curl command requesting a web page from steves-internet-guide.com:
The first line of the response is mandatory and consists of the protocol ( HTTP/1.1),response code (200)and description (OK).
All subsequent lines are optional
The headers shown are:
CONTENT-Type -This is Text/html which is a web page. It also includes the character set which is UTF-8.
Connection – Is keep-alive which means that the connection is held open.
Keep-Alive -This setting as shown is a timeout which says that the server will keep the connection open for 15 seconds then close it. See here for more details.
All of the other headers are self explanatory.
Here is another screen shot (partial) of a site returning JSON data , notice the Content type.
For normal users headers are of no interest as they are hidden and created automatically either by the browser (request headers) or web server (response headers).
However web developers and IOT developers will need to be able to set these headers manually.
The most common tool used for this is the command line curl utility but extensions are available for Google Chrome and Firefox that also let you set request headers.
Just do a search for chrome headers extension or Firefox headers add-on.
HTTP headers convey addition information between the client and web server and are used on the request and response.
Although they are inserted automatically by the web browser and web server it is important for web developers,IOT developers and engineers to have a basic understanding of them as it is sometimes necessary to manually add headers when making API requests using command line tools like curl.
It is also sometimes necessary to process response headers in IOT applications.