The browser
HTTP: methods, status codes, headers
The language a browser uses to ask a server and get an answer: a method + URL in the request, a status code in the response.
Why it exists
Browsers and servers are written by different people in different languages: the page in JavaScript, the server in Python, Go or PHP. To understand each other they need a shared format for the conversation. HTTP answers the questions every request raises: what we want to do (the method), with what (the URL), what format the data is in (headers), and how it all ended (the status code). Without status codes the client would have to guess from the response text whether the fish was found, the server broke, or you just need to log in.
Where it's used
- Every fetch request: the method and URL in the request,
res.statusandres.okin the response. - REST APIs:
GET /api/fish— the list,POST /api/orders— create,PUT /api/fish/3— update,DELETE /api/fish/3— delete. - Different UI reactions:
401— send to the login page,404— show "not found",5xx— "try again later" and a "Retry" button. - Headers:
Content-Typesays what format the body is in,Authorizationcarries a login token. - The Network tab in the developer tools: it shows the method, status and headers of every request.
Syntax
GET /api/fish → 200 OK
POST /api/orders → 201 Created
GET /api/fish/999 → 404 Not Found
res.ok // true for 200–299Examples
const res = new Response(null, { status: 404, statusText: "Not Found" });
res.status; // → 404
res.ok; // → false
res.statusText; // → "Not Found"const ok = (status) => new Response(null, { status }).ok;
ok(200); // → true
ok(201); // → true — "created"
ok(204); // → true — "done, no body"
ok(404); // → false
ok(500); // → falseres.ok means "status from 200 to 299"function whoIsToBlame(status) {
if (status >= 500) return "server";
if (status >= 400) return "client";
if (status >= 300) return "redirect";
return "all good";
}
whoIsToBlame(503); // → "server"
whoIsToBlame(401); // → "client"
whoIsToBlame(201); // → "all good"const headers = new Headers({ "Content-Type": "application/json" });
headers.get("content-type"); // → "application/json" — case doesn't matter
headers.has("Authorization"); // → falseResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
An HTTP request is text: the first line is POST /api/orders HTTP/1.1, then headers like Content-Type: application/json, an empty line and the body. A response is built the same way: the line HTTP/1.1 201 Created, headers, body. Modern versions of the protocol (HTTP/2, HTTP/3) pack this into a binary format, but the meaning is the same.
Methods are a convention: GET only reads and shouldn't change anything (it can be repeated and cached), POST creates something new, PUT and PATCH update, DELETE deletes. GET has no body — parameters go into the URL after ?.
A status code is a three-digit number whose first digit tells the main thing: 2xx — success (200 OK, 201 Created, 204 No Content), 3xx — redirect, 4xx — the client made a mistake (400 bad request, 401 not logged in, 403 forbidden, 404 no such thing), 5xx — the server broke (500, 503). That's why res.ok is simply status >= 200 && status <= 299.
Headers are "name: value" pairs, and the case of the name doesn't matter.
Traps
When to pick something else
Don't build logic on the error text when there's a status code: if (res.status === 404) is more reliable than searching the body for "not found". And don't invent your own meanings for codes ("200 with an error: true field") — clients, libraries and developer tools will have a harder time understanding what happened.
Practice
Lessons where this appears
- Why doesn't fetch fail on a 404?Why? · Chapter 21 · The network: fetch and HTTP
- The case of the fish named undefinedInvestigation · Chapter 21 · The network: fetch and HTTP
- A customer's ordersLab · Chapter 21 · The network: fetch and HTTP
- The Moody ServerBoss · Chapter 21 · The network: fetch and HTTP
- How a real app gets plannedTale · Chapter 29 · Final project: a weather app
- Building the weather appWorkshop · Chapter 29 · Final project: a weather app