HTTP methods, idempotency, PUT vs PATCH
Safe methods (GET, HEAD, OPTIONS) do not change state, and idempotent ones (those plus PUT and DELETE) leave the same state after N calls as after one. PUT replaces a resource fully and PATCH changes a part, so PATCH's idempotency depends on the operation ("set" or "add"). Idempotency concerns state, not the response: a repeated DELETE may return 404. POST is not idempotent, so payments need an Idempotency-Key.
API · Methods
A client repeated a request after a timeout. Will a second record be created or not?
A mobile client sent a request, the network dropped and the app retried it automatically. For GET that costs nothing. For POST /orders it can be a second order, and for DELETE a different response to the same request. The answer to "what happens on a retry" depends on the method, and it is exactly what an interviewer asks for.
This lesson sorts the methods by two properties, safety and idempotency, explains why they are defined that way and shows how to check them in a test.
How it works
RFC 9110 describes methods through their semantics, not through what a particular server does. It is a contract between the client, proxies and caches: they decide from it what may be cached, prefetched and retried automatically.
- GET reads a resource, HEAD does the same without the body, OPTIONS asks about capabilities (the CORS preflight is an OPTIONS too).
- POST creates a resource or triggers an action, PUT replaces a resource fully, PATCH changes a part, DELETE removes.
A safe method is not expected to change state: GET, HEAD, OPTIONS (and TRACE). That is why a browser or a crawler may call them without asking. An idempotent method has the same effect on the server after one call and after N: all safe methods plus PUT and DELETE. POST is not idempotent, and PATCH is not guaranteed to be (RFC 5789).
Safe, idempotent and all the others
Note that idempotency concerns the server state, not the response. A repeated DELETE may give 404 instead of 204 and the method is still idempotent, because the resource is gone either way. Clients and proxies need it to retry safely when a response did not arrive: it is unknown whether the server executed the request.
Example: a retry after a timeout
There is a user 7. The client repeats PUT first, since the body fully describes the final state:
PUT /v1/users/7 HTTP/1.1
Content-Type: application/json
{ "firstName": "Olena", "lastName": "Koval", "nickname": "olena" }
HTTP/1.1 200 OK (the second identical PUT: 200 again, same state)
The second PUT changes nothing, so a retry is safe. The same goes for DELETE: the state after a repeat is the same, only the response changes.
curl -i -X DELETE "$BASE/v1/orders/17" -H "Authorization: Bearer $TOKEN"
# HTTP/1.1 204 No Content
curl -i -X DELETE "$BASE/v1/orders/17" -H "Authorization: Bearer $TOKEN"
# HTTP/1.1 404 Not Found (the response differs, the server state is the same)
Now POST. It says "create something new" and the server cannot know this is the same intent. A test that proves it:
import { test, expect } from '@playwright/test';
test('PUT twice leaves one state, POST twice leaves two', async ({ request }) => {
const body = { firstName: 'Olena', lastName: 'Koval', nickname: 'olena' };
await request.put('/v1/users/7', { data: body });
const first = await (await request.get('/v1/users/7')).json();
await request.put('/v1/users/7', { data: body });
const second = await (await request.get('/v1/users/7')).json();
expect(second).toEqual(first);
const a = await request.post('/v1/orders', { data: { sku: 'A1', qty: 1 } });
const b = await request.post('/v1/orders', { data: { sku: 'A1', qty: 1 } });
expect((await a.json()).id).not.toBe((await b.json()).id); // two orders
});
The first steps compare the state before and after a repeat, the last one shows that two POSTs create two orders. That is not a bug: POST is designed that way. The bug is a payment or an order duplicated where the client has a right to retry (see Idempotency-Key in the fintech lesson).
PUT versus PATCH and the limits of the rule
PUT sends the full representation, so a repeat gives the same state. PATCH describes a change, and its nature depends on the format. "Set tier = gold" is idempotent, while "append a tag to the list" or "increase the counter by 1" is not. That is why the table says "not guaranteed", not "no".
A repeated POST and PUT
PUT /users/7 twice
The same final state. The client may retry when it got no response.
POST /orders twice
Two new orders unless the server tells a retry from a new intent.
Other limits: safety is a commitment of the service, not a guarantee. If GET /orders/17/cancel cancels an order, that is a defect: a browser prefetch or a cache may call it. Idempotency does not protect against races either: two different PUTs from two clients can overwrite each other (that is what If-Match and ETag are for). A body in GET and DELETE has no defined semantics (RFC 9110), so do not rely on it.
Common mistakes
- "Idempotent" means "the same response". No: a 404 on a repeated DELETE is fine, and a test should not fail on a different status if the contract allows it.
- Treating PATCH as idempotent. Then a test repeats a "+1" operation and is surprised by the difference.
- Writing a GET that changes data. A cache and a prefetch will fire it without the user's intent.
- Leaving POST unprotected against a repeat in a payment flow.
Check yourself
A client repeated DELETE /orders/17 and got 404. Is that a bug?
Answer
No, if the contract allows it. The server state is the same: the order is gone. Idempotency describes state, not the response code. It would be a bug if the repeat deleted something else or returned 500.
Why is it safe to repeat PUT but not POST?
Answer
PUT names the resource address and the full state: after the second call the resource is the same. POST means "create", and the server cannot know it is a retry of the same intent, so it creates another record. Idempotency-Key is the protection.
Is PATCH {"views": 1} idempotent?
Answer
It depends on the semantics. If it means "set views = 1", yes. If it is an increment "+1", no. The contract has to say so, and a test should repeat the request and compare the state through GET.
Idempotency-Key, for PATCH I clarify whether the operation is "set" or "add", and I treat a GET that changes state as a defect.