Coding

PUT vs PATCH: Differences and When to Use Each

Use PUT when you are sending the complete new version of a resource, and PATCH when you are sending only the changes to apply to it. PUT is always idempotent; PATCH is not guaranteed to be, because its body is a set of instructions rather than a finished copy.

PUT vs PATCH at a glance

PUTPATCH
Request bodyThe complete new state of the resourceA set of instructions describing how to modify the resource
Fields you leave outReplaced or reset, because the body is the whole resourceLeft unchanged
IdempotentYesNot guaranteed; depends on the patch
Safe (read-only)NoNo
Defined inRFC 9110 (HTTP Semantics)RFC 5789
Allowed in HTML formsNoNo
Body formatThe resource's own representationA patch format such as JSON Merge Patch or JSON Patch

The core difference: replace vs modify

RFC 5789 explains why PATCH exists in one sentence: PUT is already defined to overwrite a resource with a complete new body, so it cannot be used for partial updates. In a PATCH, the enclosed entity contains a set of instructions describing how the resource should be changed.

MDN states the contrast the same way: a PATCH serves as a set of instructions for modifying a resource, whereas PUT represents a complete replacement.

Take a user record stored on the server (example data):

{"name": "Alex", "email": "[email protected]", "status": "active"}

To suspend this user with PUT, you send every field, including the ones that did not change:

PUT /users/123 HTTP/1.1
Host: api.example.com
Content-Type: application/json

{"name": "Alex", "email": "[email protected]", "status": "suspended"}

With PATCH you send only the change. This is the shape of the request in MDN's example:

PATCH /users/123 HTTP/1.1
Host: api.example.com
Content-Type: application/json

{"status": "suspended"}

If you sent that same one-field body with PUT, a server that follows the definition would treat it as the entire new resource. name and email would be removed or reset. That data loss is the most common mistake people make with PUT, and it is the main reason to reach for PATCH.

For the other half of the PUT story, who chooses the URL and how it differs from POST, see PUT vs POST.

Idempotency: why PUT is always idempotent and PATCH is not

An idempotent request has the same effect whether you send it once or ten times. MDN says a PUT request is always idempotent: repeating it leaves the resource in the same state. A PATCH request may not be.

MDN gives a concrete reason. If a resource includes an auto-incrementing counter, a PUT overwrites the counter because it replaces the whole resource, but a PATCH may not. Two kinds of patch show the difference:

  • Set a value: {"status": "suspended"}. Applying it twice gives the same result as applying it once. This patch is idempotent in practice.
  • Apply a relative change: an instruction such as "add 1 to loginCount", or an array append. Applying it twice gives a different result from applying it once. This patch is not idempotent.

RFC 5789 puts it formally: PATCH is neither safe nor idempotent as defined by HTTP, but a PATCH request can be issued in such a way as to be idempotent. The practical consequence is that a client or proxy cannot blindly resend a PATCH after a dropped connection the way it can with PUT. Whether a retry is safe depends on what the patch says.

MDN also notes that, like POST, a PATCH request can have side effects on other resources.

Preventing lost updates with ETag and If-Match

Because a PATCH describes a change relative to the current state, two clients patching the same resource at the same time can interact in ways neither expected. RFC 5789 says clients using this kind of patch application should use a conditional request, so that the patch applies only if the resource is still in the state the client last saw. The client can do this with a strong ETag in an If-Match header on the PATCH request.

The RFC's own example (section 2.1) shows the pattern. The request carries If-Match: "e0023aa4e", and a successful response is:

HTTP/1.1 204 No Content
Content-Location: /file.txt
ETag: "e0023aa4f"

The new ETag lets the client send its next conditional request. The RFC says 204 is used here because the response does not carry a message body.

The RFC also requires that the server apply the entire set of changes atomically. A patch that fails halfway must not leave the resource partly modified.

Patch formats: JSON Merge Patch vs JSON Patch

PATCH itself does not say what the body looks like. The body format is identified by the Content-Type, and there are two common standard choices for JSON.

JSON Merge Patch (RFC 7396)

Media type application/merge-patch+json. The patch looks like a partial copy of the document, and it is merged into the target. Null values in the patch have a special meaning: they remove that member from the target. Example (derived from the rules in RFC 7396):

Original: {"name": "Alex", "email": "[email protected]", "phone": "555-0100"}
Patch:    {"email": "[email protected]", "phone": null}
Result:   {"name": "Alex", "email": "[email protected]"}

Its main limitation is arrays. RFC 7396 says it is not possible to patch part of a target that is not an object: if the patch value is an array, it replaces the whole array. It also cannot set a member to an actual null, because null means remove. The RFC notes the format is not appropriate for all JSON syntaxes.

JSON Patch (RFC 6902)

Media type application/json-patch+json. The patch is an array of operations, each with an op that must be one of add, remove, replace, move, copy or test. This example is from Appendix A.2 of the RFC, adding an array element:

Original: { "foo": [ "bar", "baz" ] }
Patch:    [ { "op": "add", "path": "/foo/1", "value": "qux" } ]
Result:   { "foo": [ "bar", "qux", "baz" ] }

JSON Patch can edit single array elements, which Merge Patch cannot. The cost is a more verbose body. Operations such as add on an array index are not idempotent, which is a good illustration of why PATCH as a whole is not.

Many APIs skip both formats and accept a plain JSON object of changed fields with application/json. That works, but its rules (for example, how null is handled) are whatever that API documents.

Status codes for PATCH errors

RFC 5789 section 2.2 lists the error cases a server should handle:

  • 400 (Bad Request): the patch document is malformed.
  • 409 (Conflict): the patch is well formed but cannot be applied to the resource's current state.
  • 415 (Unsupported Media Type): the server does not support the patch format sent.
  • 422 (Unprocessable Entity): the document is syntactically valid but semantically wrong for this resource.

For the broader list of codes, see HTTP status codes explained.

Discovering whether a server supports PATCH

RFC 5789 says the Accept-Patch header should appear in the OPTIONS response for any resource that supports PATCH. It lists the patch media types the server understands. PATCH may also appear in the Allow header even if Accept-Patch is absent. The RFC's example of an Allow value is GET, PUT, POST, OPTIONS, HEAD, DELETE, PATCH.

In a browser, MDN notes that PATCH is not used for user-initiated actions such as form submission, so you send it with fetch:

await fetch("/users/123", {
  method: "PATCH",
  headers: { "Content-Type": "application/merge-patch+json" },
  body: JSON.stringify({ status: "suspended" }),
});

One practical note: cross-origin requests from a page with a method other than GET, HEAD or POST trigger a CORS preflight, so the API must allow PATCH in its CORS response headers. MDN's PATCH page mentions Access-Control-Allow-Methods as one way servers advertise support.

Which to use: quick decision rules

  • The client holds the full, current resource and wants to store it: PUT. Saving a settings document or uploading a file to a known path are typical.
  • Changing one or a few fields: PATCH. You avoid sending unchanged data and avoid overwriting fields another client changed meanwhile.
  • Need safe automatic retries: PUT, or a PATCH that only sets values rather than applying relative changes.
  • Several clients edit the same resource: use PATCH or PUT with ETag and If-Match so conflicting writes are rejected instead of silently winning.
  • Editing items inside arrays: JSON Patch, or have the API expose the items as their own resources.

Neither method is better in general. Check the API you are calling: some services only implement one of them, and some treat PUT as a merge, which departs from the definition.

Frequently asked questions

Is PATCH idempotent?
Not by definition. RFC 5789 says PATCH is neither safe nor idempotent, but a PATCH request can be issued in such a way as to be idempotent. Setting a field to a fixed value is idempotent in practice; an instruction like 'add 1 to the counter' is not. PUT, by contrast, is always idempotent.
Can I use PUT for a partial update?
Not reliably. PUT is defined to overwrite a resource with a complete new body, so a server that follows the definition treats a PUT with one field as the whole new resource. Some APIs merge on PUT anyway, but that is a private convention you must confirm in that API's documentation.
Which status code should a successful PATCH return?
Any success code is allowed. RFC 5789 and MDN both show 204 (No Content) with a Content-Location header and a new ETag, because there is no need to send a body back. Returning 200 (OK) with the updated resource is also common.
How does a client know a server supports PATCH?
Send an OPTIONS request. RFC 5789 says the Accept-Patch header should appear in the OPTIONS response for any resource that supports PATCH, listing the patch media types it understands. PATCH may also appear in the Allow header.
Does PATCH work in an HTML form?
No. MDN lists PATCH as not allowed in HTML forms and says browsers do not use it for user-initiated actions. Developers send it from script with fetch.

Sources