Back to results for “Error Handling”
Node.js › HTTP & Web Servers

What is the difference between HTTP methods GET, POST, PUT, PATCH, and DELETE, and what does 'idempotent' mean in this context?

Intermediate

Answer

GET retrieves a resource without side effects; POST creates a new resource or triggers a non-idempotent action; PUT replaces a resource entirely with the provided representation; PATCH applies a partial update to a resource; DELETE removes a resource. An operation is idempotent if performing it multiple times has the same effect as performing it once -- GET, PUT, and DELETE are conventionally idempotent (repeating a DELETE on an already-deleted resource typically still results in it being gone), while POST is conventionally not (submitting the same POST twice, like a payment, could create two separate resources).
app.get('/users/:id', getUserHandler);       // idempotent, no side effects
app.put('/users/:id', replaceUserHandler);   // idempotent, full replacement
app.patch('/users/:id', updateUserHandler);  // partial update
app.post('/users', createUserHandler);       // NOT idempotent, creates a new resource each time
app.delete('/users/:id', deleteUserHandler); // idempotent
Real-world example A payment API deliberately implements its charge-creation endpoint as POST (correctly non-idempotent by default) but requires an explicit idempotency key parameter specifically to make repeated identical requests safe, since a naive retry of a plain POST could otherwise result in a customer being charged multiple times.

Common follow-ups: Why does the idempotency convention for these HTTP methods matter for how proxies and clients handle automatic retries?;How does the earlier-discussed idempotency key pattern for payments relate to this HTTP-method-level idempotency convention?