esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

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.

13 min readFree

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.

Key words

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

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?

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.

Text
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

(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?

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. 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. 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. 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. 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. 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?

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

How is this lesson?

Last updated on 17 November 2026

Learn with me

Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.