DEV Community

Cover image for How an HTTP Session Works
Archit Sharma
Archit Sharma

Posted on

How an HTTP Session Works

In this article, we will discuss How an HTTP Session Works.
HTTP consist of three phases:

  1. Connection Phase - The client initiates a TCP connection (or another designated transport protocol) to the server.
  2. Request Phase - The client sends an HTTP request and waits for the HTTP response from the server.
  3. Response Phase - The server processes the request and sends an HTTP response to the client.

NOTE: For a comparison of how connections are handled in different HTTP versions, see my article on HTTP Version History.

The initial connection phase

It is always the client which establishes the connection.
From HTTP/1.0 through HTTP/2, TCP was used to initiate connections in the transport layer. This changed with HTTP/3, which replaced TCP with QUIC over UDP.

TCP uses default port 80 for an HTTP server on a computer. Underneath, TCP goes through a process known as the three-way handshake to establish a connection. You can learn more about this process in RFC 793, Section 3.4.

Client HTTP Request

Now user-agent (typically a web browser, but can be anything else) can send the HTTP Request.
HTTP request consists of text directives (human-readable text commands), separated by CRLF (special character sequence that means end of the line) divided into three blocks:

  1. The first line consists of a request method, followed by the path of the document, and the HTTP protocol version.
  2. The second line consists of HTTP Headers, which are key-value pairs of text information that are sent with every HTTP request and response.
  3. Third block is an optional data block, which is mainly used by the POST method.

Here is an example of HTTP Request:

GET / HTTP/1.1
Host: example.com
Accept-Language: fr

Enter fullscreen mode Exit fullscreen mode

Request Methods:

Request Methods indicate the purpose of the request and what is expected if the request is successful. These request methods are commonly referred to as HTTP verbs, though they can also be nouns. Most common methods are GET and POST.

NOTE: We will explore HTTP methods in more detail in upcoming articles.

Server HTTP Response

The web server processes the HTTP request from the client and returns a response.
Just like an HTTP request, an HTTP response also consists of text directives, separated by CRLF, and is divided into three blocks:

  1. The first line, known as the status line, consists of an acknowledgment of the HTTP version used, followed by a response status code and a brief message.
  2. The second line contains HTTP headers, which give the client information about the data being sent. These headers form a block that ends with an empty line.
  3. The third line is a data block, which contains the optional data.

Example of HTTP Response:

HTTP/1.1 200 OK
Date: Mon, 27 Oct 2025 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Sat, 25 Oct 2025 19:15:56 GMT
Content-Length: 88
Content-Type: text/html
Connection: Closed

<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

HTTP status codes:

HTTP status codes indicate whether a specific HTTP request has been successfully completed. Responses are categorized into five types: informational responses, successful responses, redirects, client errors, and server errors.

NOTE: We will explore HTTP status codes in more detail in upcoming articles.

Top comments (0)