The HTTP

Ever wondered how your browser fetches a webpage or how data flows between servers? Understanding HTTP is key to decoding the web’s communication!

What is HTTP?

HTTP stands for Hypertext Transfer Protocol. It is used to exchange information between a client and a web server. This is a client-server protocol, meaning that the client initiates a request, and the server responds to that request accordingly.

HTTP is an application layer protocol that uses TCP or TLS to send messages over a secured connection.

What is a protocol?

A protocol is a set of rules, a contract between two devices. It’s like two devices agreeing on what language to communicate in. There are various types of protocols, and they dictate how to interpret and format data so that other devices can understand it.

HTTP Session flow?

There are three phases in the HTTP session.

  1. The client establishes the appropriate transport layer connection with the server, usually TCP.
  2. The client sends the request and waits for the answer.
  3. The server processes the request and responds to the client appropriately by sending the status code and data.

Before HTTP/1.1, the connections were closed after a single request and response cycle, the client has to go through all the above three phases to make a new request. In and After HTTP/1.1, the underlying transport layer connection stays open so we can perform the 2 and 3 phases any number of times.

HTTP Message?

Before HTTP/2, messages in the HTTP protocol were text-based, making them easy to read and understand. However, in HTTP/2, the messages are encapsulated in binary framing, which makes them more challenging to interpret. Despite this change, the underlying structure of the messages remains the same. So, let’s use HTTP/1.1 for better readability.

Structure of HTTP?


Article content
HTTP Message

The structure of the request and response of the HTTP message is the same.

  1. Start line: The start line consists of three parts. For the request, it describes the type of request method, request endpoint, and HTTP version. For the response, it describes the HTTP version, status code, and status message.
  2. Headers: consist of key-value pairs that contain metadata describing the message, such as the type of encoding used, the length of the message, and the source address.
  3. Empty line: This empty line indicates the metadata is complete.
  4. Body: This is the data associated with the message. For example, a client sending a post request might contain a username and password or a client receiving resources from the server in response.

POST / HTTP/1.1\r\n
Host: developer.mozilla.org\r\n
User-Agent: curl/8.6.0\r\n
Accept: */*\r\n
Content-Type: application/json\r\n
Content-Length: 345\r\n
\r\n
{
  "data": "ABC123"
}        

That is how a request looks, separated by carriage-return and line-feed characters. Now we know which part is the start line, headers, and body while parsing.

HTTP Request?

A Post request after the user submits the form on the web page.

POST /users HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 50

name=FirstName%20LastName&email=bsmth%40example.com        

Request Start-line?

<method> <request-target> <protocol>        

  1. Method: The HTTP method defines the purpose of the message. For instance, GET indicates that the client wishes to receive resources, while POST signifies that the client intends to send data to the server.
  2. Request-target: It is the URL to which the client wants to send or receive data.
  3. Protocol: The HTTP version indicates the expected version to use for response.

Request Headers?

Headers are the metadata about the message.

Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 50        

These headers are used as an example for the above request. Headers are case-insensitive and are separated by a colon (:). Each header has a distinct meaning. For example, a content-type header describes the format of the data. content-length header describes the length of the body and many more.

Request body?

This is an HTTP body used as an example for the above request.

name=FirstName%20LastName&email=bsmth%40example.com        

Only PUT, POST and PATCH methods have the request body. A request body is what carries the information to the server. The information we are looking at is in URL-encoded form and its length is 50. This information is provided in headers. so that the server knows how to decode the body and the expected length of the body.

HTTP Response?

The response from the servers is pretty much the same but the start line indicates:

  1. HTTP version: The HTTP version of the response.
  2. Status code: Integers represent the status of a response, with various status codes indicating different outcomes. A status code of 200 means that the request was successful. A code of 404 indicates that the URL the client is requesting cannot be found. A code of 500 signifies that there is an issue on the server side. There are additional status codes as well.
  3. Status Message: It is a brief text that describes the status of the response and is easily understandable.

“Keep exploring, keep coding, and stay curious!”

To view or add a comment, sign in

More articles by Rakesh Noothi

Explore content categories