HTTP Request-Responce Cycle

Ganesh GhadageGanesh Ghadage
5 min read

When you search something on browser you will get some results or if you search for any websites you get a web page of that particular website. Do you know how this happens? What magic browser does to get the result? This happens because of something called as Request-Response Cycle. Our browser request something to the server and server responds with some result which browser shows us on screen. Response can be anything a search result, web page, movie, images, etc. let's go deep dive into this as understand how really this happens.

As shown in above image a browser (client) sends a HTTP Request to the server and server responds back with HTTP Response completing the request response cycle.

What is Client?

The client is piece of hardware or a software which asks a information to the server, in our case a browser. Client can be anything browser, terminal, API testing tools like Postman or even some cases a one server can acts as client to another server.,

What is Server?

A server is a piece of hardware or software that provides a information or the resource, data, functionality to the client.

What is HTTP?

HTTP (HyperText Transfer Protocol) are set of rules with defines how we can transfer hypertext documents over the internet. HTTP is responsible for determining how the web works.

HTTP Request

When client wants to ask something to the server it sends an HTTP request to the server. HTTP Request typically consists of three parts:

  1. A request line containing three pieces of information-

    • The HTTP verb (also called as HTTP method) determining whether to recieve or send the information.

    • The URI from/to which receive/send the information.

    • HTTP version (HTTP/1.1 or HTTP/2)

  2. Headers, there are a key value pairs which contains a mera data about the request and client.

  3. An optimal body; Request only contains a body if we want to send some payload to the server in case of modifying or updating the data.

HTTP Response

When server receives a request from client and completes its processing it sends its response back to the client with response data in the HTTP Response format. HTTP Response typically consists of three parts:

  1. A response status line containing three pieces of information

    • HTTP version (HTTP/1.1 or HTTP/2)

    • status code, a three digit number

    • status, a user-friendly message which denotes what status code means

  2. Headers, a key-value pair containing meta data similar to request headers.

  3. Payload, the data which client has requested.

HTTP methods

Depending on either you want to send data to server or receive data from it you need to specify what HTTP verb to use. In other words we can say that HTTP verb indicates the server what action to perform. There are lot of types of HTTP verb available, some of mostly used verbs are mentioned below:

  • GET: to get (READ) the data from server

  • POST: to send (CREATE) data to server with the information from request

  • PUT: to UPDATE the entire data on server with the information from request

  • PATCH: to UPDATE the part of data on server with the information from request

  • DELETE: to delete (DESTROY) the data from server

HTTP Headers

HTTP headers let us to pass the some additional information to the server. Headers are categorised into three categories.

  1. General Headers: these are associated with every request and response.

  2. Request Headers: these are associated with request.

  3. Response Headers: these are associated with response.

HTTP status code

Every HTTP response has its status code which determines the status of the request. These 3 digit codes acre categorised into 5 classes, where 1st digit represents the class.

5 Classes of status codes

  1. 1xx (information Code) - status code between 100 to 199 represents the information like request received, processing, etc.

  2. 2xx (Success Code) - status code between 200 to 299 represents the request is successful, processed, etc.

  3. 3xx (Redirects Code) - status code between 300 to 399 represents the request is redirected to another url or server.

  4. 4xx (Client Error Code) - status code between 400 to 499 represents error from client like unauthorised, bad syntax, incorrect data, etc.

  5. 5xx (Server Error Code) - status code between 500 to 599 represents error from server like server failed to fulfill request.

HTTP cats this website represents various HTTP codes and their definition.

HTTP Request-Response Cycle

As discussed at start when we send HTTP request form client to server, server responds back in the format of HTTP response. Now as we have comprehensive idea about HTTP let's discuss the flow of request-response cycle.

  1. When search for anything on browser, browser builds and sends HTTP Request to server.

  2. Server receives the request and process it.

  3. Server formats the data and builds response in HTTP Response format and sends it to client.

  4. Once client receives the response it shows it on to the screen.

Summary

The article explains the HTTP Request-Response Cycle, detailing how browsers communicate with servers to retrieve information like web pages and search results. It describes the roles of clients and servers, the structure of HTTP requests and responses, and the significance of HTTP methods and headers. The article also categorizes HTTP status codes into five classes, explaining their meanings. This process illuminates how web interactions are structured and executed.

0
Subscribe to my newsletter

Read articles from Ganesh Ghadage directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ganesh Ghadage
Ganesh Ghadage