Lesson 194 of 296
HTTP methods, status codes and headers
What you will learn
- The structure of HTTP requests and responses
- HTTP methods and their properties: safe and idempotent
- Status codes and when to use each
- Important headers
- PUT vs PATCH, 401 vs 403, and other trick questions
On this page
Requests and responses
HTTP (Lesson 1) is text-based request and response:
POST /api/orders HTTP/1.1
Host: shop.example.com
Content-Type: application/json
Authorization: Bearer eyJhbGciOi...
Idempotency-Key: 7c1d9e0a-...
{"productId":"mug-01","quantity":2}HTTP/1.1 201 Created
Content-Type: application/json
Location: /api/orders/ord_123
{"id":"ord_123","status":"pending"}A request has a method, a path, headers and an optional body. A response has a status code, headers and an optional body.
Methods
| Method | Purpose | Safe | Idempotent | Body |
|---|---|---|---|---|
| GET | Read a resource | Yes | Yes | No |
| HEAD | Like GET but headers only | Yes | Yes | No |
| OPTIONS | Ask what is allowed (used by CORS preflight, Lesson 199) | Yes | Yes | No |
| POST | Create, or perform an action | No | No | Yes |
| PUT | Replace a resource completely (or create at a known URL) | No | Yes | Yes |
| PATCH | Partly update a resource | No | Not guaranteed | Yes |
| DELETE | Delete a resource | No | Yes | Usually no |
- Safe means it does not change anything on the server (read-only). Browsers and crawlers may call safe methods freely, so never change data in a GET (a crawler following links could delete records).
- Idempotent means doing it once or many times has the same effect on the server. Deleting order 5 twice leaves it deleted; PUT with the same body twice gives the same result. POST "create order" twice creates two orders. Idempotency matters for safe retries (Lesson 195).
PUT vs PATCH (very common): PUT sends the whole resource and replaces it; missing fields are removed or reset. PATCH sends only the fields to change. Most APIs use PATCH for edits.
Status codes
| Range | Meaning |
|---|---|
| 1xx | Informational (rare; 101 Switching Protocols is used for WebSockets) |
| 2xx | Success |
| 3xx | Redirection or "use your cache" |
| 4xx | Client error: the request was wrong |
| 5xx | Server error: the server failed |
The codes you must know:
| Code | Name | Use |
|---|---|---|
| 200 | OK | Successful GET, PUT or PATCH with a body |
| 201 | Created | A POST created a resource; include a Location header |
| 202 | Accepted | Accepted for later processing (a background job) |
| 204 | No Content | Success with no body (often DELETE) |
| 301 / 308 | Moved Permanently / Permanent Redirect | URL changed forever (308 keeps the method and body) |
| 302 / 307 | Found / Temporary Redirect | Temporary move (307 keeps the method and body) |
| 304 | Not Modified | Your cached copy is still valid (Lesson 198) |
| 400 | Bad Request | Malformed request (invalid JSON, missing fields) |
| 401 | Unauthorized | Not authenticated: no valid login or token |
| 403 | Forbidden | Authenticated but not allowed |
| 404 | Not Found | The resource does not exist (also used to hide resources the user may not see) |
| 405 | Method Not Allowed | e.g. DELETE on an endpoint that does not support it |
| 409 | Conflict | Conflicts with current state: duplicate email, version conflict |
| 410 | Gone | Existed but permanently removed |
| 412 | Precondition Failed | An If-Match check failed (optimistic locking, Lesson 225) |
| 413 | Content Too Large | Body too big (upload limits) |
| 415 | Unsupported Media Type | Wrong Content-Type |
| 422 | Unprocessable Content | Valid JSON but fails validation rules |
| 429 | Too Many Requests | Rate limited; include Retry-After (Lesson 238) |
| 500 | Internal Server Error | An unexpected bug |
| 502 | Bad Gateway | A proxy got a bad response from the upstream server |
| 503 | Service Unavailable | Overloaded or under maintenance; temporary |
| 504 | Gateway Timeout | A proxy timed out waiting for the upstream server |
401 vs 403 (asked constantly): 401 means "who are you? log in"; 403 means "I know who you are, and you cannot do this". The name "Unauthorized" for 401 is historically confusing: it really means unauthenticated.
400 vs 422: many APIs use 400 for all invalid input; others use 400 for malformed requests and 422 for well-formed input that breaks validation rules. Either is fine if consistent.
Should "not allowed to see it" be 403 or 404? Returning 404 for another tenant's record avoids revealing that it exists (Lesson 203).
Important headers
| Header | Purpose |
|---|---|
Content-Type | Format of the body: application/json, text/html; charset=utf-8, multipart/form-data |
Accept | Formats the client wants back |
Authorization | Credentials: Bearer <token> or Basic ... |
Cookie / Set-Cookie | Cookies sent by the browser / set by the server |
Cache-Control, ETag, If-None-Match | Caching (Lesson 198) |
Location | URL of a created resource or a redirect target |
Retry-After | When to retry after 429 or 503 |
Origin, Access-Control-Allow-Origin | CORS (Lesson 199) |
User-Agent | The client software |
X-Request-Id (or the traceparent standard) | An ID to trace one request through logs and services |
Idempotency-Key | Makes POST retries safe (Lesson 195) |
Header names are case-insensitive.
How to say it in an interview
"GET, HEAD and OPTIONS are safe and idempotent; PUT and DELETE are idempotent but not safe; POST is neither, and PATCH is not guaranteed to be idempotent. PUT replaces a whole resource while PATCH changes some fields. I return 201 with a Location header for creation, 204 for empty success, 400 or 422 for invalid input, 401 when not authenticated, 403 when authenticated but not allowed, 404 also to hide other tenants' records, 409 for conflicts, 429 with Retry-After for rate limits, and 5xx only for server failures. Key headers include Content-Type, Authorization, Cache-Control and ETag, Location and a request ID for tracing."
Key words
| Word | Meaning |
|---|---|
| Method | The action of a request (GET, POST...) |
| Safe method | Does not change server state |
| Idempotent | Same effect whether done once or many times |
| Status code | A three-digit result code |
| Header | A name and value giving extra information about a request or response |
| 401 vs 403 | Not authenticated vs not allowed |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.