The HTTP Protocol

Hypertext transfer protocol (HTTP) is the core communications protocol used by all of today’s web applications. HTTP uses a message based model in which a client sends a request message and the server returns a response message. HTTP is a connectionless protocol. HTTP does not keep a connection between client and server. HTTP client sent a request to the server and disconnects from server and wait for the response. Then server processes the request and re-establishes the connection with the client to send a response back.

HTTP Request

A typical HTTP request is as follows

GET https://www.google.com/ HTTP/1.1
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Cookie: NID=111=EQraZU6M1YUVbgmDmX5pd4nY0AW_Mcx5aWESUF0IAtM_LhVG9b1eS5BS1QOHiNGOZYc18Oc9nqb4N_0i0x80JBm55l1XG09WdUy0np47HXzQSudz77ffECFR2DOrhaozENu9_8mSJ9WMJP7wCJpfRJihrUuFwKvqgowphg7sJTt0Q_c7eLPu0BNnQrBeD4OZYJUMXgH0B66myTeXpns143_I2UF9I5zwzgKuE9iW36CZCU; CONSENT=YES+IN.en+20170730-08-0; 1P_JAR=2017-9-11-5; SID=JwXw8_vGlJLJvnqCpwHWz_20KjirKT1cpUZ0CIssOgPdLxq6bxA-xjJkjs6bQN0bGhz46Q.; HSID=AqFT6ElGGVCgSr72w; SSID=AdVd1vj4LK7BBhWra; APISID=j8eJHZNwrBW3WXmN/A2s1TVYVBOl0s3DrP; SAPISID=U5Y4q0Bs6LWeTH9R/AwP60cJC64fd281oL; SIDCC=AE4kn79CPSO5gYTQzqGAb4ZvinlfPXDT18PQ35G1p4_QDKaMmPR8FUM9V218diBh0QJ5nN04YRd1EpJV8npZ
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Host: www.google.com

The first line of this request consists of 3 parts. In the above example first part is GET, second part is https://www.google.com/ and third part is HTTP/1.1.The first part represent a verb indicating HTTP method, second part represents the requested URL and third part represents the HTTP version. The only HTTP versions in common use on the Internet are 1.0 and 1.1.

HTTP Response

A typical HTTP response is as follows

HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Referrer-Policy: no-referrer
Location: https://www.google.co.in/?gfe_rd=cr&dcr=0&ei=gzW2WYeiFp_rugS51IiADg
Content-Length: 272
Date: Mon, 11 Sep 2017 07:04:35 GMT
Alt-Svc: quic=":443"; ma=2592000; v="39,38,37,35"

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="https://www.google.co.in/?gfe_rd=cr&amp;dcr=0&amp;ei=gzW2WYeiFp_rugS51IiADg">here</A>.
</BODY></HTML> 

The first line of every HTTP response consists of three parts. In the above example the first part is 'HTTP/1.1', second part is '302' and third part is 'Found'. The first part represents the HTTP version, second part is the numeric status code indicating the result of the request and third part is the textual reason phrase further describing the status of the response.

HTTP Methods

Some of the common HTTP methods are given below
  1. GET
    The GET method is used to retrieve resources. Requests using GET should only retrieve data and should have no other effect on the data.
  2. POST
    The POST method is designed to perform actions.
  3. HEAD
    HEAD function is same as GET. Except that HEAD will not return the message body.
  4. PUT
    PUT function replaces all current representation of target resource with the content contained in the body of the request.
  5. DELETE
    DELETE function deletes the target resource.
  6. OPTIONS
    Describes the HTTP methods that are available for a particular resource.

HTTP Headers

HTTP headers provide required information about request or response. There are three types of HTTP headers.
  1. General headers

    General headers can be used with request or response. Some of the general headers are given below
    • Connection : It tells whether the connection will keep alive or close after completing the message transmission.
    • Content-Length : It specifies the length of the message body.
    • Content-Type : Specifies the type of the content in the message body.
    • Content-Encoding : It specifies what type of encoding applied to the content of the message body.
    • Transfer-Encoding : It is different from Content-Encoding. It specifies what type of encoding applied to the message body.
  2. Request headers

    Some of the request headers are given below.
    • Accept : It tells to the server what type of content will client accept.
    • Accept-Encoding : It tells to the server what kind of content encoding will accept by the client.
    • Cookie : It submits cookies to the server that the server previously issued.
  3. Response headers

    Some of the response headers are given below.
    • Cache-Control : It passes caching directives to the browser (for example, private).
    • Location : It is used in redirection responses (those that have a status code starting with 3) to specify the target of the redirect.
    • Expires : It tells the browser for how long the contents of the message body are valid. The browser may use the cached copy of this resource until this time.