What does an HTTP request contain, and what does the server send back in the response?
A request has a method (GET, POST and so on), a URL (the path plus any query string), headers with metadata such as Host, Authorization, Content-Type and cookies, and an optional body, for example the JSON of a POST. The response has a status code such as 200 or 404, headers such as Content-Type, Set-Cookie and Cache-Control, and a body, usually JSON or HTML. HTTP is stateless: every request carries everything the server needs to handle it.
Full answerHide full answer3 min read
What actually goes over the wire
POST /api/orders?source=app HTTP/1.1
Host: shop.example.com
Content-Type: application/json
Authorization: Bearer eyJhbGciOi...
Content-Length: 25
{"item":"kettle","qty":1}
HTTP/1.1 201 Created
Content-Type: application/json
Location: /api/orders/42
{"id":42,"status":"created"}
The same exchange in Node.js
import http from "node:http";
const server = http.createServer((req, res) => {
let body = "";
req.on("data", (chunk) => (body += chunk));
req.on("end", () => {
console.log(req.method, req.url, req.headers["content-type"], body);
res.writeHead(201, { "Content-Type": "application/json", Location: "/api/orders/42" });
res.end(JSON.stringify({ id: 42, status: "created" }));
});
});
server.listen(3000, async () => {
const res = await fetch("http://localhost:3000/api/orders?source=app", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ item: "kettle", qty: 1 }),
});
console.log(res.status, res.headers.get("location"), await res.json());
server.close();
});POST /api/orders?source=app application/json {"item":"kettle","qty":1}
201 /api/orders/42 { id: 42, status: 'created' }
The client–server model: the client (a browser, a mobile app, another service) always starts the conversation; the server only ever responds. Because HTTP is stateless, "who is this?" has to travel with every request, in a cookie or a token (questions 4 and 21).
Follow-ups
- HTTP/1.1 vs HTTP/2 vs HTTP/3? HTTP/2 sends many requests over one connection at once (multiplexing) with compressed headers. HTTP/3 does the same over QUIC, on UDP, which avoids head-of-line blocking when packets are lost.
Content-TypevsAccept?Content-Typesays what format the body being sent is in;Acceptsays what format the client wants back.