Part 6 of 8 · 2 min read
The request itself: HTTP
The shape of an HTTP message, status codes, and the versions of the protocol.
Think of a form with three parts: a first line that says what is wanted, a set of labelled notes, and a space for a longer message. A reply has the same shape.
HTTP
HTTP (Hypertext Transfer Protocol) is the request-and-response protocol of the web. A client sends a request message to a server, and the server sends back a response message. When HTTP runs on TLS, it is called HTTPS.
Example: a browser asks for a page, and the server replies with the page.
An HTTP message in version 1.1 is text with three parts:
GET /products/42 HTTP/1.1 <- start line: what is wanted
Host: www.example.com <- headers: labelled notes
Accept: application/json
HTTP/1.1 200 OK <- start line of the reply: result
Content-Type: application/json <- headers
<- empty line, then the body
{"id": 42, "category": "Laptop", "price": 999}
- The start line says what is wanted, or what happened.
- Headers are labelled notes about the message, such as the kind of content.
- The body carries data. It is optional.
HTTP is stateless: each request must carry everything the server needs. The server does not remember earlier requests. This is why level 0 said that stateless servers are easier to scale: HTTP itself is stateless, and the design only has to keep it that way.
The answer starts with a status code, a number that says what happened:
| Range | Meaning | Example |
|---|---|---|
| 200 to 299 | Success | 200 OK |
| 300 to 399 | Redirect: look elsewhere | 301 Moved Permanently |
| 400 to 499 | Client error: the request was wrong | 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found |
| 500 to 599 | Server error: the server failed | 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable |
The ranges guide what to do next. A client error will usually fail again if repeated, because the problem is in the request. So it should not be retried unchanged. A server error may pass, because the fault may be fixed by the time of the next attempt. So a retry can make sense. Level 8 returns to this.
Versions. HTTP 1.1 can send only one request at a time on a connection and waits for each reply. A page that needs many images may open several connections. HTTP 2 is a binary format that can carry many requests at once on a single connection. HTTP 3 runs over UDP and builds its own reliability, to avoid some of the weaknesses of TCP. Use HTTPS by default.
Interview angle
Answer in your head first, then open it.
What happens when a user types a URL and presses enter?
A strong answer goes through DNS, TCP, TLS and HTTP in order, says where saved copies help, and counts the round trips.
Check yourself:
- A request fails with status 404. Should the client retry it unchanged?
- A request fails with status 503. Why might a retry help?