Back

28th October 2024

#Programming

Understanding HTTP: statelessness, methods, headers and many more!

Blog image

Hello! In this article I would like to tell you some important information about HTTP protocol, which is really significant thing in topics like computer networks or web development. Let's start!

What is HTTP?

It is one of many protocols, which belongs to application layer in TCP/IP model. It means, that HTTP is protocol used by most applications for providing user services or exchanging application data over the network connections established by lower-level protocol (which belongs to Transport layer, Internet layer etc.).

In other words HTTP is using for communication between client and server. With this protocol clients communicate with servers to order files that make up websites and provide the necessary information e.g. content entered in forms. It determines the form of the client's requests for data and the form of the server's response to these requests.

Other important information about HTTP:

What does it mean that HTTP protocol is stateless?

Each request from the client is independent and does not contain information about previous requests. Neither server nor client does not store previous requests details and each request is treated as new. It seems, that for example information about currently authenticated user is not possible to maintain in entire application lifecycle. But nowadays there are solutions for this type of problems. For example, we have cookies in the case with authentication. However, remember, that HTTP itself is stateless.

Types of HTTP connections

Persistent connection

Non-persistent connection

Methods

HTTP protocol uses various methods to effectively manage communication between client and server. Each method has specific purpose and determine own way to process request.

Methods idempotency

What actually means, that method is idempotent? It is property, that ensures, that performing the same operation multiple times has the same effect as performing it once. In other words, idempotent operation gives the same result no matter how many times it is repeated.

In context of HTTP is means, that sending a couple of identical requests does not change server state and the result compared to previous request is identical.

Examples

Headers

Headers are key element of HTTP protocol, which enable to send many additional information between client and server. Headers contains request's or response's metadata such as information about data type, authentication, encoding, session duration, cache settings and much more. Each header consist of key-value pair and is sending before request or response content.

Header: value

Nowadays, in the newest versions of protocol there are over 80 headers available, but I would like to list you a couple of the most important ones. We can divide them into following categories.

General Headers

Can occur both in request and in response. They are referring to the general qualities of communication.

Request Headers

Used by client to deliver additional information about request and preferences.

Response Headers

Deliver information about server and response to the client.

Security Headers

Helps with application protection against typical hacker's attacks like XSS or CSRF.

Headers related to caching and session control

Response status codes

Response status codes are actually three-digit numbers, which informs about result of processing particular request sent by client to the server. Enables to find out whether request was successful or if there was a problem. These codes are divided into five main categories:

Cookies

Cookie is a small piece of data, which server sends to a user's web browser to store information about state, session, preferences and user's actions. They are saved on the device in special browser's file and automatically sending back to the server during next requests. Typically they are used for authentication purposes and to store user's preferences.

When you visit the website, the server may send a cookie using the Set-Cookie header in the response. An example header looks like this:

Set-cookie: sessionId=abc123; Expires=Wed, 09 Jun 2024 10:18:14 GMT; Path=/; Secure; HttpOnly

When the client is sending request, browser can automatically add cookie to it with the Cookie header. Example:

Cookie: sessionId=abc123

Thanks to this server can "recognize" user and for example, keep his login or preferences.

HTTPS

HTTPS is HTTP protocol with security. Protection with TLS (Transport Layer Security) protocol is intended to prevent interception of communication between client and server (man in the middle attack) or even modification of transmitted data before it reaches its destination.

Unlike HTTP, which listens on port 80, HTTPS servers listen on port 443. URLs starts with https://, while HTTP addresses start with http://.

Back to articles