Sub topic 7
Reading status codes
This chapter assumes you can read a basic HTTP request and response, as covered in the earlier chapter on HTTP messages. You should also be comfortable running curl in a terminal.
What a status code tells you
Every HTTP response begins with a status line. It holds the protocol version, a three-digit status code, and a short reason phrase.
HTTP/1.1 404 Not FoundThe code is what programs read. The reason phrase is only a human hint, and HTTP/2 and HTTP/3 drop it entirely. The first digit sets the class of the response.
| Class | Meaning | Typical use |
|---|---|---|
| 1xx | Informational | The request was received, and processing continues |
| 2xx | Success | The request worked |
| 3xx | Redirection | The client must take another step, usually follow a new URL |
| 4xx | Client error | The request was wrong in some way |
| 5xx | Server error | The request looked valid, but the server failed |
The 4xx and 5xx split tells you where to look first. A 4xx means fix the request. A 5xx means the problem is on the server side.
Codes you will meet most often
200 OK: the request succeeded, and the body holds the result.201 Created: a new resource now exists. Servers often send aLocationheader with its URL.204 No Content: success with no body, common after a delete.301 Moved Permanentlyand308 Permanent Redirect: the resource has a new URL for good. Caches and search engines may remember it.302 Foundand307 Temporary Redirect: the move is temporary.304 Not Modified: your cached copy is still valid. The caching chapter covers this.400 Bad Request: the server could not parse or accept the request.401 Unauthorized: the request lacks valid credentials. Despite the name, it means "not authenticated".403 Forbidden: the server knows who you are and refuses anyway.404 Not Found: no resource exists at that URL.429 Too Many Requests: you hit a rate limit.500 Internal Server Error: the server hit an unexpected failure.502 Bad Gatewayand504 Gateway Timeout: a proxy or gateway got a bad answer, or none in time, from the server behind it.503 Service Unavailable: the server is overloaded or down for maintenance.
How redirects work
A redirect is a 3xx response with a Location header. The browser reads the header and sends a new request to that URL.
HTTP/1.1 301 Moved Permanently
Location: https://example.com/new-pageYou can watch this with curl. The -i flag prints the headers, and -L tells curl to follow redirects.
curl -i http://example.com/old
curl -iL http://example.com/oldChoosing a code in your own server
Send the code that matches what happened. Use the Node.js server from the earlier chapter as a base.
import { createServer } from "node:http";
const server = createServer((req, res) => {
if (req.url === "/") {
res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" });
res.end("Home\n");
} else if (req.url === "/old") {
res.writeHead(301, { Location: "/" });
res.end();
} else {
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
res.end("Not found\n");
}
});
server.listen(3000);Common mistakes
- Returning
200 OKwith an error message in the body. Clients, caches, and monitoring tools trust the code, so they will treat the failure as a success. - Mixing up 401 and 403. Use 401 when credentials are missing or invalid, and 403 when they are valid but not permitted.
- Using a permanent redirect while testing. Browsers cache 301 and 308 responses, so a wrong one can stick around.
- Blaming the application for a 502 or 504. These usually point to the upstream server behind a proxy.
Exercise
A browser requests /account without logging in and gets a 401 response. After logging in as a normal user, it requests /admin and gets a 403. What does each code tell you?
Show answer
The 401 means the server did not have valid credentials, so the user was not authenticated. The 403 means the server knew who the user was but did not allow access to /admin.