HTTP Methods and Status Codes
HTTP methods and status codes are the vocabulary of every API. Interviewers check whether you know which methods are safe and idempotent, and whether you return the right status code instead of 200 for everything.
What you will learn
- Know what GET, POST, PUT, PATCH and DELETE mean
- Explain safe and idempotent methods
- Pick the right 2xx, 3xx, 4xx and 5xx code
- Know the difference between 401 and 403, and 400 and 422
Key concepts
Safe method
A method that only reads and does not change server state: GET, HEAD, OPTIONS. Crawlers and prefetchers may call safe methods freely.
Idempotent method
Calling it once or ten times leaves the server in the same state. GET, PUT, DELETE are idempotent. POST is not: two POSTs create two orders. PATCH is not guaranteed to be idempotent.
PUT vs PATCH
PUT replaces the whole resource with the body you send. PATCH applies a partial change, such as updating only the email.
Status code classes
1xx informational, 2xx success, 3xx redirect, 4xx the client made a mistake, 5xx the server failed.
Step by step
The codes you will use every day
- 200 OK: success with a body
- 201 Created: a POST created something; send a Location header
- 204 No Content: success, no body (often DELETE)
- 301 / 308: moved permanently; 302 / 307: temporary redirect (307/308 keep the method)
- 304 Not Modified: the cached copy is still fresh
- 400 Bad Request: malformed input; 422 Unprocessable Content: valid JSON but fails validation
- 401 Unauthorized: not logged in; 403 Forbidden: logged in but not allowed
- 404 Not Found, 405 Method Not Allowed, 409 Conflict (duplicate email, version clash)
- 429 Too Many Requests: rate limited; send Retry-After
- 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable, 504 Gateway Timeout
Making POST safe to retry
Networks fail mid-request. If a client retries a payment POST you may charge twice. The usual fix is an Idempotency-Key header: the server stores the key with the result and returns the stored result for a repeat.
Examples
Express handlers with correct codes
app.post('/users', async (req, res) => {
const exists = await db.user.findUnique({ where: { email: req.body.email } })
if (exists) return res.status(409).json({ error: 'Email already registered' })
const user = await db.user.create({ data: req.body })
res.status(201).location('/users/' + user.id).json(user)
})
app.delete('/users/:id', requireAdmin, async (req, res) => {
await db.user.delete({ where: { id: req.params.id } })
res.sendStatus(204)
})Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Write the status code for: wrong password, expired token, user lacks admin role, duplicate username.
- Explain why DELETE is idempotent even though the second call returns 404.
- Design an idempotency-key flow for a payment endpoint.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
What is the difference between 401 and 403?
401 means the server does not know who you are: no credentials or invalid ones. 403 means it knows who you are but you are not allowed to do this. Logging in again fixes a 401, never a 403.
Is PUT idempotent? Is PATCH?
PUT is idempotent because it replaces the resource with the same body each time. PATCH may or may not be: "set email to x" is idempotent, "add 10 to balance" is not.
When do you return 502, 503 and 504?
502: a proxy got an invalid response from the upstream. 503: the service is temporarily down or overloaded (maintenance, no healthy instances). 504: the proxy timed out waiting for the upstream.
Summary
Use methods for their meaning: GET reads, POST creates, PUT replaces, PATCH updates part, DELETE removes. Know which are safe and idempotent, protect POST with idempotency keys, and return precise status codes so clients can react correctly.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus