What Is HTTP? How Browsers and Servers Talk
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.
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.
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.
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.