Course contents · Lesson 6 of 22

What Is HTTP? How Browsers and Servers Talk

The Web and Staying Safe · Lesson 6 of 22 · 9 min Foundations

You tap a link and get a page saying "404 Not Found". On a sale day, a busy shop's site shows "503 Service Unavailable". Those numbers are replies in HTTP, the language browsers and websites use to talk. This lesson answers what is HTTP: what requests and responses contain, what status codes mean, and how sites remember you between clicks.

What HTTP is and where it fits

HTTP (Hypertext Transfer Protocol) is the set of rules browsers and web servers use to ask for and send pages, pictures and data; hypertext is text with links. As in lesson 4, the browser is the client: it asks first, and the server answers each request with one response.

HTTP defines only what the messages say. They travel over a connection set up by TCP, or by QUIC for the newest version (lesson 5), usually to port 80. HTTPS, on port 443, is the same messages inside an encrypted connection (lesson 8), so this lesson applies to both.

In the flowchart, the browser asks for /menu and gets 200 OK with the page, then asks for /missing, which the site lacks, and gets 404 Not Found. Notice that the server answered both times: a 404 is a reply, not an outage.

Flowchart with the browser, the client that asks, on the left and the web server, the waiter that answers, on the right, time running downwards. Step 1: the browser sends GET /menu, please send the menu page. Step 2: the server finds /menu, because a page exists at that path. Step 3: the server replies 200 OK with the page, and the browser shows the menu. Step 4: the browser sends GET /missing, a path the site does not have. Step 5: the server finds no such path. Step 6: the server replies 404 Not Found with a short error page. A note says a 404 is still an answer: the server is up and replied
A 404 means the server is working and answered "not here".

Anatomy of a request and a response

The request

A request is a few lines of text, shown here as HTTP/1.1 writes them. The first line holds the method, a verb saying what you want; the path, the part of the web address after the site name, such as /menu; and the HTTP version.

Four methods cover most of the web. GET fetches something, like reading the menu. POST sends data for the server to act on, such as a login form or an order. PUT stores the data you send at that path, replacing what was there. DELETE removes whatever is there.

Next come headers, Name: value lines that add details, like notes on an order slip. Host names the site, since one server can host many; User-Agent names the program asking; Accept lists formats it can read. A blank line ends them. Then comes the optional body: a GET usually has none, while a POST or PUT carries its data there.

The response

A response starts with a status line: the version, a three-digit status code and a short reason phrase for humans, as in HTTP/1.1 200 OK. HTTP/2 and HTTP/3 have no reason phrase, so tools such as curl show just HTTP/2 200.

Its headers describe the body: Content-Type says what it is, such as text/html for a web page, and Content-Length can give its size in bytes. After a blank line comes the body: the page, image or data you asked for.

Two panels of HTTP/1.1 text with every line labelled. The request, from browser to server: GET /menu HTTP/1.1, the method, the path and the version; then headers giving extra details, Host: example.com, User-Agent: curl/8.7.1 and Accept: */*, saying which site, who asks and what it reads; then a blank line that ends the headers, and no body, because a GET only asks. The response, from server to browser: HTTP/1.1 200 OK, the version, the status code and the reason phrase; then headers that describe the body, Content-Type: text/html and Content-Length: 1256, its type and size in bytes; then a blank line; then the body, the HTML of the page itself. Values are illustrative, and HTTP/2 and HTTP/3 carry the same parts in a compact binary form
Both messages: first line, headers, blank line, then an optional body.

Status codes: the waiter's replies

Every status code has three digits; the first names its family, so even an unfamiliar code tells you roughly what happened. A 1xx family of interim notes also exists, but you will rarely meet it.

2xx means success: 200 OK is the everyday one, and 201 Created means a POST or PUT made something new. 3xx is a redirect, "ask over there": with 301 (moved for good) or 302 (moved for now), a Location header gives the new address, and browsers follow it by themselves.

4xx means the request was wrong: 404 Not Found (no such path) or 400 Bad Request (the server couldn't understand it). 5xx means the server failed on a request that looked fine: 500 Internal Server Error when something broke in the kitchen, or 503 Service Unavailable when it is overloaded or down for maintenance.

The family tells you what to do next. Resending the same 4xx request usually gets the same answer, so fix the request; a 5xx is the server's problem, so retrying later may work.

Chart of four rows, one per status code family. 2xx, success: 200 OK and 201 Created; the waiter says here's your dish, the request worked. 3xx, redirect: 301 Moved Permanently and 302 Found, moved for now; the waiter says order that at counter 2, go to another address. 4xx, client error: 400 Bad Request and 404 Not Found; the waiter says that's not on our menu, fix the request first. 5xx, server error: 500 Internal Server Error and 503 Service Unavailable; the waiter says sorry, the kitchen failed, try again later. A note says 1xx codes are interim notes you rarely see
The first digit says whose problem it is: 4xx the request's, 5xx the server's.

HTTP is stateless: cookies and tokens

HTTP is stateless: each request must make sense on its own, and the protocol remembers nothing from earlier ones. Picture a waiter who forgets you as soon as they walk away: order dessert, and they can't tell which table's bill to add it to.

Websites fix this with a cookie, a small piece of text the server asks your browser to keep. A response sets one with a header such as Set-Cookie: session=7f3a9c (an illustrative value), and the browser then adds Cookie: session=7f3a9c to its requests to that site, like a table ticket shown with each order.

Apps and scripts often carry a token instead: a long string issued when you logged in, sent with each request, usually in an Authorization header. Lesson 9 shows how logins create both. Stateless doesn't mean the site forgets your basket: the site stores it, and the cookie or token says which one is yours.

HTTP/1.1, HTTP/2 and HTTP/3

HTTP versions change how messages travel, not what they mean. HTTP/1.1 (1997) sends the text lines above, and each connection handles requests one after another, so browsers open several. HTTP/2 (2015) packs the same parts into compact binary pieces and runs many requests at once over one TCP connection. HTTP/3 (2022) does the same over QUIC. Methods, paths, headers and status codes mean the same in all three.

Check yourself

A friend says a site is down because a page showed "404 Not Found". Are they right?

No. A 404 is a response, so the server is up and answering; it just has nothing at that path.

A ticket site answers "503 Service Unavailable" during a sale. Should you change your request or try again later?

Try again later. A 5xx means the server failed on a fine request, here from overload; changing the request fixes a 4xx.

You add a book to your basket, then open the basket page. How does a stateless server know which basket to show?

Your browser sends the site's cookie with the second request, and the server looks up the session it names; HTTP itself remembered nothing.