HTTP
HTTP is the request/response protocol the web uses. A client sends a method, URL, headers, and optional body; the server answers with a status, headers, and a body.
Detailed Explanation
A request line is GET /users/7 HTTP/1.1 plus headers (Host, Accept, Authorization, Cookie, Content-Type). GET and HEAD should not have a meaningful body. POST, PUT, PATCH, DELETE may. HTTPS is HTTP over TLS; the JavaScript API is still fetch("https://...").
Status codes are grouped: 2xx success (200, 201, 204), 3xx redirect (301, 302, 304), 4xx client (400, 401, 403, 404, 409, 429), 5xx server (500, 502, 503). fetch follows redirects by default (redirect: "follow"). 401 means unauthenticated; 403 means authenticated but not allowed.
Headers are metadata. Cache-Control and ETag drive caches. Set-Cookie is a response header the browser stores; JavaScript sees document.cookie only for non-HttpOnly cookies. Content-Type describes the body. CORS adds Origin on the request and Access-Control-* on the response.
HTTP/1.1 is text and can pipeline poorly; HTTP/2 multiplexes streams on one connection; HTTP/3 uses QUIC. Your fetch code does not change. The browser picks the version.
Idempotency: repeating GET, PUT, DELETE should be safe in the protocol's meaning. POST is not. Retries on POST can double-charge unless you send an idempotency key.
What it costs you: treating HTTP as "just JSON". Binary bodies, streaming, and status handling are part of the protocol. Logging bodies that contain tokens is a leak.
Reach for HTTP for request/response APIs. Reach for WebSockets when you need a long-lived, server-push channel. Reach for the History API when you change the URL without a full navigation.
Code Examples
const res = await fetch("https://httpbin.org/get", {
method: "GET",
headers: { Accept: "application/json" },
});
console.log(res.status);
console.log(res.headers.get("content-type"));
const body = await res.json();function classify(status) {
if (status >= 200 && status < 300) return "ok";
if (status >= 400 && status < 500) return "client";
if (status >= 500) return "server";
return "other";
}
console.log(classify(201)); // => ok
console.log(classify(404)); // => clientCommon Gotchas
`GET` with a body is not interoperable
put data in the query or use `POST`.
`204` has no body
calling `res.json()` fails. Check status first.
Status 304 is a cache hit
`fetch` cache mode decides whether you see it.