Cloud VPS from $1.98/mo · pay with Bitcoin, Ethereum, Monero, USDT or Solana · paid in crypto
DevOps

PUT or PATCH for API updates: semantics, retries, conflicts

PUT or PATCH for API updates: what RFC 9110 and RFC 5789 require, JSON Merge Patch versus JSON Patch, safe retries, and ETag checks that stop lost updates.

CloudSat teamNovember 18, 20267 min read
Abstract illustration of two document shapes approaching one record, one replacing it whole and one fitting in as a small glowing piece
On this pageWhat each method promises

Two support agents open the same customer record. One changes the plan, the other fixes the email address, and both press save within a second. Whether the record ends up with both changes, one of them, or a field silently wiped depends on three choices in your API: the method that carries the update, the format of the body, and whether the server checks that the client edited the current version. HTTP standards already answer all three.

What each method promises

PUT, defined in RFC 9110, requests that the state of the target resource be created or replaced with the state in the request body. If the resource did not exist and the PUT creates it, the server answers 201 Created; if it replaced an existing one, 200 OK or 204 No Content. PUT is idempotent: the intended effect of sending the same request twice is the same as sending it once, which is why a client may repeat it automatically after a network failure.

PATCH, defined in RFC 5789, carries a set of instructions describing how to modify the resource. The RFC states plainly that PATCH is neither safe nor idempotent, although a given PATCH request can be issued in a way that is. The server must apply the whole patch atomically: if any part cannot be applied, none of it is.

PUT PATCH POST
Body means The complete new state Instructions for a change Data for the target to process
Idempotent by definition Yes No No
Creates resources Yes, at the URI the client names Only if the server allows it Yes, at a URI the server chooses
Safe to retry blindly Yes Depends on the patch No, unless you add a key

A PUT replaces everything

Because the body of a PUT is the new state, a missing field is a deleted field. This request sets four fields, and any field it leaves out is reset or removed:

curl -X PUT https://api.example.com/v1/customers/42 \
  -H 'Content-Type: application/json' \
  -d '{"name":"Ada Lovelace","email":"ada@example.com","plan":"pro","tags":["vip"]}'

That makes PUT a good fit when the client naturally holds the whole document: a settings object, a configuration file, a page in an editor. It is a poor fit for large records edited a field at a time, because every client must send back fields it never displayed. The classic bug is an old mobile app that does not know a newer field and erases it on every save.

Two formats for PATCH

"PATCH with some JSON" is not a format. Pick one of the two standard media types and say so in Content-Type.

JSON Merge Patch (RFC 7396)

A merge patch looks like a partial copy of the resource. Members present in the patch replace the target's values, null removes a member, and anything absent stays as it was:

curl -X PATCH https://api.example.com/v1/customers/42 \
  -H 'Content-Type: application/merge-patch+json' \
  -d '{"plan":"team","phone":null}'

It is easy to read and easy to build from a web form. The limits come from the same simplicity: you cannot set a field to null (null means remove), and you cannot change part of an array. A tags array in a merge patch replaces the whole array.

JSON Patch (RFC 6902)

A JSON Patch is an array of operations applied in order: add, remove, replace, move, copy and test. If one operation fails, evaluation stops and the patch as a whole is not applied:

[
  { "op": "test", "path": "/plan", "value": "pro" },
  { "op": "replace", "path": "/plan", "value": "team" },
  { "op": "add", "path": "/tags/-", "value": "beta" }
]
curl -X PATCH https://api.example.com/v1/customers/42 \
  -H 'Content-Type: application/json-patch+json' \
  -d @patch.json

The test operation makes the change conditional on a value, and /tags/- appends to the array instead of replacing it. The price is verbosity.

JSON Merge Patch JSON Patch
Media type application/merge-patch+json application/json-patch+json
Body looks like The resource A list of operations
Set a field to null Not possible Yes
Edit inside an array No, arrays are replaced Yes, by index or append
Built-in condition None test operation

Advertise what you accept. RFC 5789 defines the Accept-Patch response header for this, sent in answer to OPTIONS and with a 415 Unsupported Media Type when a client sends a format the server does not support:

HTTP/1.1 415 Unsupported Media Type
Accept-Patch: application/merge-patch+json, application/json-patch+json

Errors that tell the client what to fix

RFC 5789 suggests specific status codes for failed patches, and following them saves your clients guesswork:

Code When
400 Bad Request The patch document is malformed
415 Unsupported Media Type The patch format is not supported; include Accept-Patch
422 Unprocessable Content The patch is valid but cannot be applied, for example it would leave the resource invalid
409 Conflict The current state of the resource makes the patch impossible
412 Precondition Failed A precondition such as If-Match did not hold
428 Precondition Required The server requires a conditional request (RFC 6585)

Stop lost updates with ETags

Back to the two agents. Without a check, both clients read version A, both write, and the second write wins silently. HTTP's answer is the conditional request. The server returns an ETag with each representation; the client sends it back in If-Match; the server applies the change only if the resource still matches, and answers 412 otherwise:

curl -si https://api.example.com/v1/customers/42 | grep -i '^etag'
# ETag: "7c4f1a"

curl -i -X PATCH https://api.example.com/v1/customers/42 \
  -H 'If-Match: "7c4f1a"' \
  -H 'Content-Type: application/merge-patch+json' \
  -d '{"plan":"team"}'
# 412 Precondition Failed if someone else saved first

The client that gets 412 fetches the record again, shows the user what changed and lets them reapply their edit. If-Match uses strong comparison, so a weak ETag (one starting with W/) never matches; generate strong ones for resources that accept writes. To make the check mandatory rather than polite, reject unconditional writes with 428 Precondition Required, which RFC 6585 created for exactly this lost-update problem.

One detail trips up servers that normalize input. RFC 9110 forbids sending a validator such as ETag in a successful PUT response unless the body was stored without any transformation. If your server trims strings or fills defaults, omit the ETag there and let the client read the new one with a GET. Responses to PATCH can carry the new ETag directly.

Retries without double effects

Networks drop responses, so clients retry. What a retry does depends on the method:

  • PUT and DELETE are idempotent. Retrying after a timeout cannot apply the change twice.
  • PATCH depends on the document. Setting plan to team twice ends in the same state; appending to an array or incrementing a counter does not. A conditional PATCH is safe to retry: after the first success the ETag changes, and the repeat fails with 412 instead of applying twice.
  • POST is not idempotent. The Idempotency-Key request header lets a client attach a unique key so the server can recognize a retry and return the first result instead of creating a second order. It is still an Internet-Draft of the IETF HTTPAPI working group (revision 08, October 2026), not an RFC, so document your own behavior if you adopt it.

A client that retries every failure immediately turns one outage into a flood; our guide to HTTP 429 and rate limits covers backoff on both sides.

Which method for which update

Situation Choose
The client holds and edits the whole document PUT
The client creates a resource at a URI it chooses PUT, answered with 201
A form changes a few fields of a large record PATCH with JSON Merge Patch
Arrays change, fields must hold null, or each step needs a guard PATCH with JSON Patch
The server assigns the ID or the request triggers an action POST, ideally with an idempotency key

On the server, apply each patch inside one database transaction, validate the resulting resource rather than only the patch, and log patch documents without secrets so you can later answer who changed a field.

FAQ

Is PATCH ever idempotent?

It can be. A merge patch that sets fields to fixed values has the same effect however often it runs, and any PATCH sent with If-Match cannot apply twice. The method itself carries no such promise, so clients should not assume it.

Can I use PUT to update only some fields?

Not under its standard meaning. A PUT body is the complete new state, and a server that treats missing fields as "unchanged" surprises every generic client and proxy. Use PATCH for partial updates.

Should PATCH accept plain application/json?

Only if you document exactly what it means, and even then clients cannot tell whether arrays are merged or replaced. Accepting application/merge-patch+json costs the same effort and settles the question.

Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.