Tuesday, 20 May 2014

HTTP Request and Response Overview

HTTP (Hyper-Text Transfer Protocol) is probably the most important of the various protocols are used for communication over the web. When a web address is typed into a web browser, the browser requests the web page using HTTP. The browser is an HTTP Client and the server is an HTTP Server. HTTP defines a set of rules regarding how messages and other data should be formatted between servers and browsers.

The HTTP protocol can be likened to a series of questions and answers, referred to respectively as HTTP Requests and HTTP Responses.

The HTTP Request

Once the connection to the server has been opened, the HTTP client transmits a request as follows:
  • An opening line
  • A number of header lines (optional)
  • A blank line
  • A message body (optional)
The opening line is generally split into three parts
  1. The name of the method (POST, GET, PUT, DELETE or HEAD). Method names are always uppercase.
  2. The path to the required server resource. The path is the part of the URL after the host name, also called the request URI (a URI is like a URL, but more general).
  3. The HTTP version being used. The HTTP version is always in uppercase and takes the form HTTP/x.x.
For example:
GET /path/to/file/index.html HTTP/1.0
This tells the server that it is receiving an HTTP request of type GET, using HTTP version 1.0, and the server resource we are using, including it's local path, is /path/to/file/index.html.

Header lines are used to send information about the request, or about the data being sent in the message body. One parameter and value pair is sent per line, separated by a colon.

For example, a header line sent from IE9 might look like:
User-Agent: Mozilla /5.0 (Compatible MSIE 9.0;Windows NT 6.1;WOW64; Trident/5.0)
Another example of  a common request header is the Accept: header, which states what sort of information will be acceptable in the server's response.

For example
Accept: text/plain, text/html
This header informs the server that the sending application can accept only plain text or HTML responses.

If the HTTP request includes a message body, the header lines describe the content of the body.

For example
Content-Type: text/plain
This header informs the server that the message body contains text
.
In an HTTP request, the message body is where user-entered data or uploaded files are sent to the server.

The HTTP Response


The server issues an HTTP response in answer to an HTTP request.

The first line of the HTTP response i known as the status line. This consists of three parts separated by spaces:
  1. the HTTP version. This is in the same format as for the HTTP request.
  2. a three digit integer, called the HTTP response status code, that gives the result of the request, and
  3. a short message known as the reason phrase describing the status code. 
Typical status lines are:
HTTP/1.0 200 OK or
HTTP/1.0 404 Not Found
The response status code and the reason phrase are intended as computer- and human-readable versions of the same message. The reason phrase may vary from server to server.

The first digit of the status code identifies the general category of response. Some commonly encountered HTTP Response Status codes are:
Status CodeExplanation
1** indicates an informational message only
100 - ContinueThe client should send the remainder of the request
101 - Switching ProtocolsThe server will switch protocols to those defined following header
2** indicates success of some kind
200 - OKThe request succeeded
204 - No ContentThe document contains no data
3** redirects the client to another URL
301 - Moved PermanentlyThe resource has permanently moved to a different URI
4** indicates an error on the client's part
401 - Not AuthorizedThe request needs user authorization
403 - ForbiddenThe server has refused to fulfil the request
404 - Not FoundThe requested resource does not exist on the server
408 - Request TimeoutThe client failed to send a request in the time allowed by the server
5** indicates an error on the server's part
500 - Server ErrorDue to a malfunctioning script, server configuration error or similar

The response may also contain header lines each containing a header and value pair similar to those of the HTTP request but generally containing information about the server and/or the resource being returned.

For example:
Server: Apache/2.4.5
Last Modified: Wed, 20 Nov 2013 13:33:59 GMT