How-To Guide

How to Choose Between PUT, PATCH, and POST

Apply HTTP method semantics correctly: PUT replaces, PATCH modifies, POST creates or acts, and each carries different idempotency guarantees.

On this page

  1. Introduction
  2. Classify the operation first
  3. Idempotency and why it matters
  4. Define PATCH semantics carefully
  5. Use conditional requests for concurrency
  6. Test retry and concurrency behavior
  7. Common pitfalls
  8. Conclusion

Introduction

HTTP methods are not interchangeable verbs. Each carries a contract about safety and idempotency, and clients, caches, and proxies rely on those contracts. A service that uses POST for everything, or PUT for a partial update, breaks retry logic and produces duplicate records when a network timeout causes a client to resend a request. Choosing the right method is a design decision with concrete consequences for reliability.

This tutorial lays out the semantics of PUT, PATCH, and POST, explains idempotency and where it matters, shows how to define PATCH bodies safely, and describes how to test that retries behave as the method promises. The examples use Node.js, but the semantics come from the specification and apply to every stack.

Classify the operation first

Start from what the request does, not from what is convenient. If the request replaces the resource at a known URL with a complete new representation, it is a PUT. If it changes only some fields of an existing resource, it is a PATCH. If it creates a resource under a collection, or triggers an action that is not a straightforward state replacement, it is a POST. This classification determines the method before any framework routing is involved.

The URL matters too. PUT targets a specific resource URL, which the client knows in advance; POST typically targets a collection and lets the server create the identifier. If the client supplies the identifier, PUT to that URL is often more accurate than POST to the collection, unless creation is not a full replacement of something that already exists.

// PUT: full replacement at a known URL
app.put('/users/:id', (req, res) => replaceUser(req.params.id, req.body));

// PATCH: partial modification
app.patch('/users/:id', (req, res) => mergeUser(req.params.id, req.body));

// POST: create under a collection, server-assigned id
app.post('/users', (req, res) => createUser(req.body));

Idempotency and why it matters

A method is idempotent when repeating the same request has the same effect as sending it once. PUT and DELETE are idempotent by definition: replacing a resource with the same representation twice leaves it in the same state, and deleting twice leaves it deleted. POST is not idempotent: sending it twice can create two resources. GET and HEAD are safe, meaning they have no side effects at all.

Idempotency matters because networks are unreliable. A client that times out mid-request cannot know whether the server processed it. With an idempotent method the client can simply retry. With POST, the client needs an idempotency key: a unique value sent with the request so the server can detect a replay, return the original result, and avoid a duplicate. This is the standard fix for retryable creation over an unreliable network.

interface IdempotencyStore {
  get(key: string): { status: number; body: unknown } | undefined;
  set(key: string, result: { status: number; body: unknown }): void;
}

async function createOnce(store: IdempotencyStore, key: string, create: () => Promise<{ status: number; body: unknown }>) {
  const seen = store.get(key);
  if (seen) return seen; // replay: return the original result
  const result = await create();
  store.set(key, result);
  return result;
}

Define PATCH semantics carefully

PATCH is deliberately flexible and therefore easy to implement unsafely. Two standard formats resolve most ambiguity. JSON Merge Patch, described in RFC 7396, treats the body as a set of fields to overwrite, with null meaning delete. JSON Patch, described in RFC 6902, is a list of operations such as add, remove, and replace, which is more precise but more verbose. Pick one, document it, and advertise it with a media type such as application/merge-patch+json.

Validate a patch before applying it. A merge patch that sets a field to an unexpected type, or a JSON Patch that references a path outside the resource, should be rejected as a 400 or 422 rather than applied partially. Reject patches that try to modify immutable fields such as an identifier or a creation timestamp. Apply the patch in a transaction so a failure midway does not leave a half-updated resource.

// JSON Merge Patch (RFC 7396): null deletes a field
function mergePatch(target, patch) {
  if (patch === null || typeof patch !== 'object' || Array.isArray(patch)) return patch;
  const out = { ...(target && typeof target === 'object' ? target : {}) };
  for (const [k, v] of Object.entries(patch)) {
    if (v === null) delete out[k];
    else out[k] = mergePatch(out[k], v);
  }
  return out;
}

Use conditional requests for concurrency

A PATCH that modifies part of a resource races with other writers. Two clients that read the same version and patch different fields can produce a result neither intended if the second write overwrites the first. Conditional requests solve this: the client sends If-Match with the ETag it read, and the server applies the patch only if the current ETag still matches, otherwise it returns 412 Precondition Failed. This is optimistic concurrency control and it fits PATCH naturally.

Return 412, not 409, when a precondition fails, because the client’s precondition is what failed. Include the current ETag so the client can re-read and retry. For PUT the same pattern applies, and it prevents a slow client from overwriting a newer version with stale data.

Test retry and concurrency behavior

Prove the idempotency claims with tests. Send the same PUT twice and assert the resource is unchanged and no error is returned. Send the same POST twice with an idempotency key and assert one resource is created and the second response repeats the first. Send two PATCHes that both carry the same stale If-Match and assert the second is rejected with 412. These tests turn the method contract into something the codebase enforces.

# idempotent PUT: repeat and compare
curl -s -X PUT -H 'content-type: application/json' -d '{"name":"a"}' http://localhost:3000/users/1
curl -s -X PUT -H 'content-type: application/json' -d '{"name":"a"}' http://localhost:3000/users/1
# POST with idempotency key: second call must not create a duplicate
curl -s -X POST -H 'idempotency-key: abc' -d '{"name":"b"}' http://localhost:3000/users

Common pitfalls

Using PUT for partial updates and silently resetting omitted fields, using POST for a full replacement, assuming POST is safely retryable, applying a patch without validation or a transaction, and ignoring concurrency so the last writer wins are the recurring mistakes. Each is a violation of method semantics or of the states those semantics imply, and the retry and conditional-request tests above expose them.

Conclusion

Choose the method from the operation: PUT replaces, PATCH modifies, POST creates or acts. Respect the idempotency contract so retries are safe, and add an idempotency key when a POST must be retryable. Define a precise patch format, validate before applying, use conditional requests for concurrent edits, and return 412 when a precondition fails. Self-check by repeating each method and asserting the outcome matches the contract. When the method matches the operation, retries and caches behave predictably.

Sources: MDN: HTTP request methods, RFC 9110: Method definitions, RFC 5789: PATCH method.

FAQ

Is PUT always a full replacement?
Yes, by definition PUT replaces the entire current representation with the one in the request. Fields omitted from the body are removed or reset, not left unchanged. Use PATCH when you want to change only some fields.
Why is POST not idempotent?
POST has no defined repeat semantics, so sending it twice can create two orders or start two jobs. When a POST needs to be safely retryable, add an idempotency key so the server can recognize a replay and return the original result.

Related Examples