HTTP: requests, responses, statuses
Read a real request and response, learn what status codes actually promise, and why HTTP pretends not to remember you.
HTTP is a text conversation on top of an established TCP connection. The client sends one request; the server sends exactly one response. A request is a request line, headers, an empty line, and an optional body. A response is a status line, headers, an empty line, and an optional body.
Connection closed. Nothing is connected yet.
- Packets sent
- 0
- Retransmits
- 0
- Status
- Not yet established
GET /articles/42 HTTP/1.1
Host: api.example.org
Accept: application/json
Authorization: Bearer eyJhbGciOi...
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: max-age=60
{"id": 42, "title": "..."}Status families tell you who should act. 2xx — it worked. 3xx — the resource moved, and the Location header says where. 4xx — the request is at fault: 400 malformed, 401 unauthenticated, 403 forbidden, 404 missing, 429 too many requests. 5xx — the server is at fault. A 502 or 503 is worth retrying; a 400 never is.
Practice
Build a correct HTTP request that creates a new order with JSON body {"item":"book"}.
Hints
- The request line answers three questions: which action, which resource, which protocol version.
- In HTTP/1.1 a request without a Host header does not tell the server which site you want.
15 XP
A POST /orders returns 503. What should the client do?
Hints
- Look at the method, not just the code: the code says what the server did, not who is at fault.
- Only retry automatically when repeating the request is safe.
13 XP
Write a loadUser(id) function that fetches /users/{id} as JSON, checks the status, and throws on 404.
Hints
- Send the Accept header from the same options object you pass to fetch.
- Return the parsed object, not the raw Response — and throw before parsing when the request failed.
16 XP