HTTP Deep Dive
The language your server speaks. Every request and response follows this exact format.
Anatomy of an HTTP Request
Every HTTP request has the same four-part structure:
POST /api/users HTTP/1.1 ← Request line: METHOD path version
Host: api.example.com ← Headers (key: value pairs)
Content-Type: application/json
Authorization: Bearer eyJhbGc...
← Blank line separates headers from body
{ ← Body (only for POST/PUT/PATCH)
"name": "Alice",
"email": "alice@example.com"
}
Anatomy of an HTTP Response
HTTP/1.1 201 Created ← Status line: version code message
Content-Type: application/json ← Response headers
Location: /api/users/42
← Blank line
{ ← Response body
"id": 42,
"name": "Alice"
}
HTTP Methods
Methods tell the server what action to perform. Use the right one — it signals intent to every developer who reads your API.
Read a resource. Never modify data. Safe & idempotent.
Create a new resource. Not idempotent — sends twice = two records.
Replace a resource entirely. Idempotent — same result every time.
Partially update a resource. Only send fields that change.
Remove a resource. Idempotent — deleting twice = still deleted.
Status Codes
The three-digit code is the first thing you check in any response. They're grouped by their first digit:
200 OK with an error message in the body. Use the correct 4xx/5xx code. Clients (and monitoring tools) check the status code first.
Important Headers
| Header | Direction | Purpose |
|---|---|---|
Content-Type | Both | Format of the body (application/json, multipart/form-data…) |
Authorization | Request | Credentials — typically Bearer <token> |
Accept | Request | What format the client wants back |
Location | Response | URL of newly created resource (with 201) |
Cache-Control | Both | Caching instructions for browsers and proxies |
X-Request-ID | Both | Unique ID for tracing a request through logs |
HTTP/1.1 vs HTTP/2 vs HTTP/3
You don't need to configure these — your platform handles it. But knowing the difference helps you understand performance:
- HTTP/1.1 — One request per TCP connection at a time. Workarounds (connection pooling, bundling) needed for speed.
- HTTP/2 — Multiplexing: many requests over one connection simultaneously. Binary protocol. Used by ~65% of the web.
- HTTP/3 — Built on UDP (QUIC). Even faster connection setup. Handles packet loss better. Still rolling out.
🧠 Check Your Understanding
Go Deeper
Primary source: MDN — HTTP request methods
Reference: httpstatuses.com — bookmark this, you'll use it weekly.
Ask your teacher: "When should I use PUT vs PATCH?" or "What's the difference between 401 and 403?"