# HTTP: How Browsers and Servers Exchange Requests and Responses

> Learn the parts of an HTTP request and response, what status codes mean, why HTTP forgets you between requests, and how HTTP/1.1, HTTP/2, and HTTP/3 differ.

**In short:** HTTP is how browsers and servers exchange requests and responses, and it forgets you between requests because it is stateless.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/http

## What you will learn

Every time you open a web page, tap a button in an app, or load a list of orders, your device and a server trade messages. The rules they follow for those messages are called HTTP. Most of the web, and most of the APIs that mobile apps use, run on it.

By the end of this lesson you will know what a request and a response contain, what the status code families mean, why HTTP forgets each visitor between requests, and how the three main HTTP versions differ. These ideas come up again in later lessons on REST, caching, and retries, so it pays to learn them well now.

- **HTTP:** Hypertext Transfer Protocol. The set of rules a client and a server follow when they ask for and send web data.
- **Protocol:** An agreed set of rules that two machines follow so they can understand each other.
- **Client:** The side that asks for something, such as a web browser or a phone app.
- **Server:** The machine that receives a request and sends back an answer.
- **Request:** The message a client sends to ask the server to do something.
- **Response:** The message the server sends back after it handles a request.
- **Method:** A word at the start of a request, such as GET, that says what the client wants done.
- **URL:** Uniform Resource Locator. The address of the thing you want, such as a page or a list.
- **Header:** A labelled line of extra information attached to a request or a response.
- **Body:** The main content of a message, such as a web page or a block of data.
- **Status code:** A three-digit number in a response that says how the request turned out.
- **Stateless:** Each request stands alone, and the server does not remember earlier requests by default.
- **Cookie:** A small piece of data the server asks the browser to store and send back with later requests.
- **Connection:** The open channel between a client and a server that carries messages.
- **TCP:** Transmission Control Protocol. A transport layer that delivers bytes in order, with resends when packets are lost.
- **QUIC:** A newer transport protocol built on top of UDP, which is a simpler transport that sends packets without a delivery guarantee.

## What are the request and the response?

HTTP has two kinds of messages. A request is sent by the client to trigger an action on the server, and a response is the answer the server sends back. Each request gets its own response, and the pair is the basic unit of the whole protocol.

- **Method:** The verb at the start of the request. It says what the client wants to happen, such as reading or changing data.
- **URL:** The address of the resource. It tells the server which page, list, or record the request is about.
- **Headers:** Extra labelled lines, such as the type of content the browser accepts or the cookie it holds. They describe the message or the sender.
- **Body:** Optional content. A GET request usually has no body, while a POST that creates something often carries one.

The response follows the same pattern. It has a version, a status code, headers, and often a body. The status code is the quickest way to learn the outcome, so most developers read it first.

> **Tip:** Read the status code before the body. A 200 with an error message inside is rare, but a 4xx or 5xx tells you at once where to look.

Under the methods sit a few common verbs. Each one describes a different kind of action, and that meaning is the reason the server treats them differently.

- **GET:** Asks for a representation of a resource. A GET should only retrieve data, and it should not change anything on the server.
- **POST:** Sends new data to the server. It often causes a change, such as creating an order or a post.
- **PUT:** Replaces the current version of the target resource with the data in the request body.
- **DELETE:** Removes the specified resource from the server.

Two more verbs appear in later lessons. PATCH changes only part of a resource, and HEAD asks for the same answer as GET but without the body.

## What are the status code families?

A status code always has three digits. The first digit groups the code into a family, so you can learn the meaning of a whole family before you learn each code. HTTP sorts all responses into five classes.

- **1xx: informational:** The request arrived and the work is continuing. These are interim messages, and most developers rarely see them.
- **2xx: success:** The request was received, understood, and accepted. The most common one is 200 OK, and 201 Created means a new resource was made.
- **3xx: redirection:** The client must take another step before the request is complete, often by going to a different URL. A 301 says the page has moved for good.
- **4xx: client error:** The request was bad or cannot be done as asked. The fault lies with the client, for example a missing login or a page that does not exist. 404 means not found and 401 means the request lacked valid login.
- **5xx: server error:** The server failed to complete a request that looked valid. The fault lies on the server side, not with the client that sent it.

The sample below turns a code into its family name. It shows how the first digit alone tells you who is at fault.

```python
def status_class(code):
    names = {1: "informational", 2: "success", 3: "redirect", 4: "client error", 5: "server error"}
    return names[code // 100]

for code in [200, 404, 503]:
    print(code, status_class(code))
```

Output:

```text
200 success
404 client error
503 server error
```

A short quiz to check the idea. Read the scenario, then pick the best answer.

**Quick check:** A user opens a saved-items page. The server replies that the login cookie is missing, so the app asks the user to sign in again. Which family fits, and whose side does the error sit on?

A. 2xx success, because the page loaded and the server did its job
B. 4xx client error, because the request lacked valid login, so the client side is at fault
C. 5xx server error, because the server refused to show the user their own items
D. 3xx redirection, because the app should move the user to a new page

**Answer:** B. A missing login is a request problem, so it belongs to the 4xx family, which blames the client side. The server did not fail, so 5xx is wrong.

## Why is HTTP stateless?

HTTP is stateless. Each request stands alone, and the server does not keep a memory of your last request by default. Yet websites still know who you are between clicks. Developers solve this by sending proof of identity with every request, or by storing the memory on the server side.

A session is the stretch of time when one user is signed in, and sessions are built on top of the stateless protocol.

Cookies are the most common way to carry state. The server can attach a Set-Cookie header to a response, and the browser then sends the stored value back in a Cookie header on each later request to that site. That is how the browser proves it is the same signed-in visitor.

```
GET /profile/ada HTTP/1.1
Host: social.example
Accept: text/html
Cookie: session=s42
```

The sample above shows a request with a Cookie header. The server reads the cookie and decides who is asking. The lines are illustrative and use a made-up site name.

Cookies are not the only option. Two other designs are common, and each one moves the memory to a different place.

- **Token:** A signed string the client keeps and sends in a header with each request. The server checks the signature, and it does not need to store a record for every visitor.
- **Server-side session:** The server keeps a record for each signed-in user. The client holds only a short ID, and the server looks up the record on each request.

```python
SESSIONS = {"s42": "ada"}

def handle(cookie):
    user = SESSIONS.get(cookie)
    if user is None:
        return (401, "log in first")
    return (200, "profile of " + user)

print(handle("s42"))
print(handle("old"))
```

Output:

```text
(200, 'profile of ada')
(401, 'log in first')
```

> **Tip:** Where you keep the state changes the design. If a session lives in one server's memory, the next request may land on a different server and look like a stranger. Shared storage or self-contained tokens avoid that problem.

## How do HTTP versions differ?

HTTP has several versions, and they share the same meanings for methods, status codes, and headers. What changes is how the bytes move over the network. A design choice about connections can therefore make a visible difference to speed, even when the code does not change.

The short version is that HTTP/1.1 sends one response at a time per connection, HTTP/2 mixes many exchanges on one connection, and HTTP/3 moves everything onto QUIC.

HTTP/1.1 introduced persistent connections, so one open channel can carry several requests and responses in turn. Before this, older versions closed the channel after each exchange, which wasted time on every new request.

This reuse is what people mean by keep-alive. When the client sends its next request on the same channel, it skips the cost of opening a new one. A single HTTP/1.1 connection still handles one response at a time, so browsers often open several connections to one site.

HTTP/2 keeps TCP but changes the format. It uses binary frames, which are small pieces of data with a label, and it can interleave requests and responses on one connection. That is called multiplexing. It also compresses repeated headers, which matters for cookies sent again and again.

HTTP/2 still has a weakness. TCP delivers bytes in order, so if one packet is lost, every stream on that connection waits for the resend. This is called head-of-line blocking.

HTTP/3 replaces TCP with QUIC, which runs over UDP. QUIC includes the encryption setup of TLS 1.3 in its handshake, and each stream is independent. A lost packet then delays only the stream that owns it, not the others.

- **Keep connections alive:** On HTTP/1.1, reuse the open connection for later requests so you do not pay the setup cost each time.
- **Expect many parallel calls:** On HTTP/2 and HTTP/3, one connection can carry many requests at once, so the app's own limits on parallel calls can become the new bottleneck.
- **Keep the meaning fixed:** Because methods and status codes stay the same across versions, a design written for one version still works on the others.

**Quick check:** A phone app sends about thirty small requests to one server over a weak mobile signal. A single lost packet should not hold up the other responses. Which version fits best?

A. HTTP/1.1 with one request per connection, because each request gets a fresh channel
B. HTTP/2 over TCP, because it multiplexes all requests on one connection
C. HTTP/3 over QUIC, because a lost packet delays only the stream that owns it
D. HTTP/1.0, because it closes each connection after one exchange and avoids waiting

**Answer:** C. HTTP/2 still stalls every stream when TCP loses a packet, while HTTP/3 limits the delay to one stream. The others either have no multiplexing or open a new channel for each request.

## Worked example: opening a profile page

Let us follow one page load from start to finish. The site and path below are made up, but the flow is how a profile page on a social site like X usually works. The browser's Network tab in its developer tools lets you watch each step as it happens.

1. **The browser builds a GET request:** You enter a profile address. The browser builds a GET request with that URL and adds headers, such as Accept, which tells the server which content types the browser prefers, and Cookie, which holds the login proof. The request has no body, because GET only asks for data.
2. **The request crosses the network:** The request travels to the server. Between the two ends there may be proxies, which are middle machines that act as gateways or caches.
3. **The server answers 200 OK:** When the profile exists and the cookie is valid, the server returns 200, which means success. The body is an HTML document, the markup that describes the page. The browser then draws the page.
4. **The page asks for data:** The page often makes more requests. Those return JSON, a text format for structured data, such as a list of posts. Each of these is its own request and its own response, with its own status code.
5. **You inspect it in the Network tab:** Each request shows up as a row with its method, URL, status, and size. Click a row to see its headers and body. This is the fastest way to check which code came back.

Now suppose the server does not give the happy answer. The same request can end in a different family, and each one tells the browser a different story.

- **401 Unauthorized:** The cookie is missing or expired, so the server cannot tell who is asking. The 401 code is a 4xx response, and the browser can show a sign-in prompt.
- **404 Not Found:** No profile matches the URL. The client asked for something that does not exist, so the fault is in the request, not in the server.
- **5xx Server Error:** The profile service crashed while it built the page. The request looked valid, so the server side is at fault and the client may try again later.

> **Tip:** When you debug a page, start with the status family. A 4xx points you to the request, a 5xx points you to the server, and a 2xx with missing data points you to the body.

## Putting it together

Now apply the whole lesson to one case. A shopping app opens a list of past orders. During a server deploy, the server answers with a 503 for a few seconds. The app has to decide what to show, and whether it can ask again.

**Quick check:** A mobile app loads a user's order list, but the server returns a 503 for a few seconds during a deploy. Which status family is this, whose fault does it signal, and why is it still safe for the app to send the same request again?

A. 4xx, the app's fault, and it is safe because the app should stop sending requests
B. 5xx, the server's fault, and it is safe because a GET only retrieves data and does not change it
C. 2xx, no fault, and it is safe because the request already succeeded
D. 3xx, the user's fault, and it is safe because the request must go to a new address

**Answer:** B. The 503 sits in the 5xx family, which points to the server. Because GET is meant only to read data, sending it again does not change anything on the server.

## Key takeaways

- **Every exchange is a pair:** A request carries a method, URL, headers, and an optional body. A response carries a status code, headers, and often a body.
- **Status codes name the fault:** 2xx means success, 3xx means go elsewhere, 4xx means the client should fix its request, and 5xx means the server failed.
- **State lives outside HTTP:** Because HTTP is stateless, login state travels in cookies or tokens, or it is stored on the server side. Choose the place with care, because it shapes how servers scale.

The next lessons build on this base. REST shapes URLs and methods into an API style, caching uses headers to reuse responses, and retries depend on knowing which requests are safe to repeat.

## Sources

- [MDN Web Docs: HTTP messages](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Messages)
- [MDN Web Docs: HTTP response status codes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status)
- [MDN Web Docs: HTTP home page](https://developer.mozilla.org/en-US/docs/Web/HTTP)
- [MDN Web Docs: How the web works](https://developer.mozilla.org/en-US/docs/Learn_web_development/Getting_started/Web_standards/How_the_web_works)
- [MDN Web Docs: Response status property](https://developer.mozilla.org/en-US/docs/Web/API/Response/status)
- [MDN Web Docs: HTTP request methods (mirror)](https://mdn2.netlify.app/en-us/docs/web/http/methods/)
- [MDN Web Docs: An overview of HTTP (mirror)](https://devdoc.net/web/developer.mozilla.org/en-US/docs/Web/HTTP/Overview.html)
- [MDN Web Docs: HTTP content repository, cookies section](https://github.com/mdn/content/blob/main/files/en-us/web/http/index.md)
- [HTTP/1.1 vs HTTP/2 vs HTTP/3 (Algomaster)](https://algomaster.io/learn/computer-networks/http-1-vs-2-vs-3)
- [HTTP/1.1 vs HTTP/2 vs HTTP/3: What Actually Changed? (DEV Community)](https://dev.to/omeiza_ahmed/http11-vs-http2-vs-http3-what-actually-changed-18c1)
- [HTTP/1.1 vs HTTP/2 vs HTTP/3 explained (ToolHQ)](https://www.toolhq.io/blog/http1-vs-http2-vs-http3)
- [The Differences Between HTTP 1.1, HTTP/2 and HTTP/3 (Accreditly)](https://accreditly.io/articles/the-differences-between-http-11-http2-and-http3)
- [HTTP 1.1 vs HTTP 2 vs HTTP 3 (Codefinity)](https://codefinity.com/blog/HTTP-1.1-vs-HTTP-2-vs-HTTP-3)
- [HTTP/1.1 vs HTTP/2 vs HTTP/3 (Forasoft)](https://www.forasoft.com/learn/video-streaming/articles-streaming/http-1-2-3-streaming)
- [Key Differences Between HTTP/1.1, HTTP/2, and HTTP/3 (CyC2018 notes mirror)](https://instagit.com/CyC2018/CS-Notes/http-versions-comparison-http11-http2-http3/)
- [HTTP/1.1 vs HTTP/2 vs HTTP/3: The Protocol Evolution Guide (Backend Bytes)](https://backendbytes.com/articles/http-protocol-evolution-guide/)
- [Complete reference of HTTP status codes (JLV extension)](https://tools.jlvextension.com/http-status-codes/)
