HTTP Headers: Understanding HTTP Part 2

HTTP-HeadersIn 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

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

HTTP-Headers

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

Connection Header

The original HTTP protocol used non persistent connections.

This meant that the client

  1. Made a request
  2. Got a Response
  3. Closed the Connection

If you consider a TCP/IP connection to be the same as a telephone connection. This means that you:

  1. Dial the number and get an answer (connection established.
  2. Say something and get an acknowledgement
  3. 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.

This header is not normally sent as the default assumed by the server is keep-alive.

Host Header

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.

Host:www.steves-internet-guide.com

User-Agent Header

This gives information about the client making the request as shown below:

user-agent-header

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.

e.g.

Accept:text/plain,text/html

For JSON encoded data the header

Accept:application/json

is used.

Other accept headers are

  • Accept-Charset:ISO-8859,UTF-8
  • Accept-Language:en-GB,en
  • Accept-Encoding:gzip,deflate

Example Headers from live session:

Request-Headers-Example

Common Response Headers

The screen shot below shows the response headers using the curl command requesting a web page from steves-internet-guide.com:

response-headers

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.

response-header-json

Setting Headers

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.

Summary

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.

<—–HTTP Basics Part 1

 

Resources

Related Tutorials

Save

Save

Save

Save

Save

Save

Please rate? And use Comments to let me know more

Leave a Reply

Your email address will not be published. Required fields are marked *