Difficulty: Intermediate
When would you use PUT, PATCH or POST? What are the differences in semantics, and what pitfalls exist with partial updates?
Let me use a simple analogy: editing a document. POST is like asking the system to create a new document for you and hand back an id. PUT is like uploading a complete new version of a specific document, replacing whatever was there. PATCH is like sending a tracked-changes note saying just change these two lines.
POST is used to create a subordinate resource in a collection (POST /orders) where the server assigns the id, and also for actions that do not map to a resource. It is not idempotent: two identical POSTs normally create two orders. Successful creation returns 201 Created with a Location header pointing at the new resource.
PUT replaces the target resource at a known URL with the representation in the body. It is idempotent: sending the same body ten times yields the same state. Because it is a full replacement, any field you omit is conceptually reset or removed. That is the classic pitfall: a client that fetched an older schema and PUTs it back may accidentally wipe fields added later. PUT can also create a resource when the client chooses the id, for example PUT /users/alice/settings, returning 201 the first time and 200 or 204 afterwards.
PATCH applies a partial modification. There are two common formats. JSON Merge Patch (RFC 7396), with content type application/merge-patch+json, is the simple one: send only the fields to change, and null means delete the field. Its limitation is that you cannot cleanly express array edits or distinguish setting null from removing. JSON Patch (RFC 6902) sends a list of operations such as add, remove, replace, move with paths, which is precise but more verbose. PATCH is not guaranteed idempotent; add an item to an array is not, while set status to shipped is. Document what you support.
How do I choose in practice? If the resource is small and clients always have the whole thing, PUT is fine and simple. If resources are large, have many optional fields or concurrent editors, prefer PATCH so that two people editing different fields do not overwrite each other. Combine with ETag and If-Match to prevent lost updates: the server rejects the change with 412 Precondition Failed if the resource changed since the client read it.
Validation is another edge. For PATCH, validate the merged result, not just the delta, so that partial updates cannot produce an invalid state. Also consider which fields are immutable, such as id or createdAt, and either ignore or reject them with 422. Never let a client patch fields like role or isAdmin; that is a mass assignment vulnerability, so whitelist the updatable fields.
POST is also the pragmatic choice for actions and for operations that fit neither, like POST /orders/42/refund. And if a client needs POST to be safe to retry, add an idempotency key.
Summary line for the interview: POST creates or triggers, PUT replaces whole, PATCH changes part, and only PUT is guaranteed idempotent among the three.
PATCH /v1/users/42 HTTP/1.1
Content-Type: application/merge-patch+json
If-Match: "v7"
{ "phone": "+91-9000000000", "nickname": null }
HTTP/1.1 200 OK
ETag: "v8"
{ "id": "42", "name": "Asha", "phone": "+91-9000000000" }
Only phone changes and nickname is removed. If-Match protects against a concurrent edit; a stale version would yield 412.
PUT, PATCH, POST, partial update