HTTP methods, status codes and idempotency
What do the main HTTP methods mean, which status codes should an API return, and what does idempotent mean?
TL;DR
An operation is idempotent if doing it once or many times leaves the server in the same state. GET, PUT and DELETE are idempotent; POST and usually PATCH aren't. Safe methods (GET, HEAD, OPTIONS) don't change state at all. This matters because networks fail and clients retry: retrying an idempotent request is harmless, but retrying a POST /payments can charge twice unless you add an idempotency key.
Key points
| Method | Use | Safe | Idempotent |
|---|---|---|---|
GET | Read | ✅ | ✅ |
POST | Create, or run an action | ❌ | ❌ |
PUT | Replace the whole resource | ❌ | ✅ |
PATCH | Partial update | ❌ | Not guaranteed |
DELETE | Remove | ❌ | ✅ |
Status codes worth knowing:
200OK,201Created (with aLocationheader),204No Content.400invalid input,401not authenticated,403authenticated but not allowed,404not found,409conflict,422validation failed,429rate limited.500server bug,502/503/504upstream or availability problems.
Idempotency keys: the client sends a unique Idempotency-Key header. The server stores the key with the result, and a retry with the same key returns the stored result instead of acting again.
Example
POST /payments
Idempotency-Key: 7f3c2a90-...
Content-Type: application/json
{ "amount": 100, "currency": "ILS" }Common follow-ups
- Is
DELETEstill idempotent if the second call returns 404? Yes. Idempotency is about server state, not the response code. - Where do you store idempotency keys? In the database (a unique constraint on the key), with a TTL, written in the same transaction as the side effect.
What interviewers look for
Tip
Define idempotency by server state, connect it to retries, and know the idempotency-key pattern for POST.
- Red flag: returning
200with{ "error": ... }in the body, or mixing up401and403.