DevAtlas

Learn

  • Dashboard
  • Learning Paths
  • All Categories
  • Topics

Practice

  • Interview Questions
  • Flashcards
  • Quizzes
  • Study Plans

Personal

  • Bookmarks
  • Notes
  • Progress
  • PDF Library
  • Settings
  • About
  1. Topics
  2. PUT vs PATCH
0%
REST APIs

PUT vs PATCH

PUT replaces an entire resource; PATCH modifies part of it. Learn the difference, when to use each, and why idempotency matters.

Intermediate22 min study10 min read
Export PDF

Prerequisites: HTTP Methods

Learning objectives

  • Explain how PUT replaces a full resource while PATCH applies a partial modification.
  • Describe the request body differences: a full representation vs a patch document (JSON Merge Patch, JSON Patch).
  • Reason about idempotency: why PUT is idempotent and why PATCH is not guaranteed to be.
  • Choose the right method and status code (200, 201, 204) for a given update scenario.

Simple explanation

Imagine you have a profile card on file at a gym. Two ways to update it:

  • PUT is like handing the desk a *brand-new, fully filled-out card* and saying *"replace mine with this one."* Whatever was there before is gone; the card you handed over becomes the whole truth.
  • PATCH is like passing a sticky note that says *"just change my phone number to 555-1234."* Everything else on the existing card stays exactly as it was.

Both update the resource, but PUT replaces the whole thing and PATCH changes only the parts you name.

Technical explanation

Both PUT and PATCH target an existing resource (e.g. /users/42), but they mean different things.

PUT carries a complete representation of the resource in its body. The server takes that body as the new state of the resource, replacing what was there. If you omit a field in a PUT body, you are effectively saying *"this field should now be absent (or reset to default)"* — not *"leave it alone."*

PATCH carries a patch document — a description of the changes to apply, not the whole resource. Only the fields you mention are touched; everything else is preserved. There are two standardized patch formats:

  • JSON Merge Patch (RFC 7386) — the body looks like a sparse version of the resource. A key with a value updates that field; a key set to null deletes it; absent keys are left unchanged. Content-Type: application/merge-patch+json.
  • JSON Patch (RFC 6902) — the body is an ordered array of operations (add, remove, replace, move, copy, test), each targeting a JSON Pointer path. Content-Type: application/json-patch+json.

Why it matters

Choosing the wrong method causes real bugs. Sending a partial object with PUT can silently wipe fields the client did not include, because PUT means *"this is the entire resource now."* Meanwhile, treating PATCH as if it were always idempotent can corrupt data when a request is retried after a timeout. Getting this right keeps your API predictable, safe to retry, and honest about what each call does.

How it works

Idempotency is the key distinction. A method is idempotent when making the same request N times leaves the server in the same final state as making it once.

  • PUT is idempotent. PUT /users/42 with the same full body, sent once or ten times, results in the same stored resource every time. That makes it safe to retry after a network failure.
  • PATCH is not guaranteed to be idempotent. It *can* be — e.g. a JSON Merge Patch that sets {"status": "active"} is idempotent. But it can also be non-idempotent, e.g. a JSON Patch operation like { "op": "add", "path": "/tags/-", "value": "vip" } appends "vip" to an array *every* time it runs, so ten retries add ten entries.

Status codes:

  • 200 OK — the update succeeded and the response includes the updated resource in the body.
  • 204 No Content — the update succeeded and there is no body to return.
  • 201 Created — a PUT to a URL that did not exist yet *created* the resource there (an "upsert"). PATCH is generally not expected to create resources this way.

Real-world analogy

Think of editing a document. PUT is *"Select All → Delete → Paste my new version."* Whatever was there is replaced wholesale. PATCH is *"Find and Replace"* on a single line — you specify only the edit, and the rest of the document is untouched. If you accidentally "Paste" (PUT) a document that is missing three paragraphs, those paragraphs are gone. If you "Find and Replace" (PATCH), you never risk the parts you did not mention.

Important terminology

  • Full representation — a complete copy of the resource's fields, the body PUT expects.
  • Patch document — a description of changes (merge patch or operation list), the body PATCH expects.
  • Idempotency — sending a request N times has the same effect as sending it once. PUT: yes. PATCH: not guaranteed.
  • Upsert — an update that creates the resource if it does not already exist; PUT to a chosen URL can behave this way and return 201.

Key definitions

PUT
Replaces the entire resource with the full representation sent in the body; idempotent.
PATCH
Applies a partial modification described by a patch document; only named fields change. Not guaranteed idempotent.
JSON Merge Patch (RFC 7386)
A PATCH format where the body is a sparse resource: values update fields, null deletes them, absent keys are untouched.
JSON Patch (RFC 6902)
A PATCH format where the body is an ordered list of operations (add, remove, replace, move, copy, test) on JSON Pointer paths.

Must memorize

Must memorize

  • PUT replaces the whole resource; PATCH changes only the fields you send.
  • PUT is idempotent; PATCH is not guaranteed to be idempotent.
  • PUT body = full representation; PATCH body = a patch document (merge patch or operation list).
  • Success codes: 200 (updated, with body), 204 (updated, no body), 201 (PUT created the resource).

Real-world example

Updating a user profile

A settings page lets a user change just their display name. Sending PATCH /users/42 with {"displayName": "Ada"} updates only that field and leaves email, avatar, and preferences intact. Using PUT here would require the client to send the entire user object, or risk erasing fields it forgot to include.

Idempotent PUT for a config document

A deployment pipeline writes the full feature-flag config to PUT /config/production on every run. Because PUT is idempotent, re-running the pipeline after a flaky network error produces exactly the same stored config — no drift, no duplicated state.

Code example

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

{
  "id": 42,
  "name": "Ada Lovelace",
  "email": "ada@example.com",
  "role": "admin",
  "active": true
}

HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": 42,
  "name": "Ada Lovelace",
  "email": "ada@example.com",
  "role": "admin",
  "active": true
}

PUT sends the complete resource; the server replaces the stored user with this body.

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

{
  "email": "ada.lovelace@example.com"
}

HTTP/1.1 204 No Content

PATCH sends only the fields to change; everything else on the user is preserved. 204 returns no body.

merge-patch.http
PATCH /api/users/42 HTTP/1.1
Host: api.example.com
Content-Type: application/merge-patch+json

{
  "email": "ada.lovelace@example.com",
  "nickname": null
}

HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": 42,
  "name": "Ada Lovelace",
  "email": "ada.lovelace@example.com",
  "role": "admin",
  "active": true
}

A JSON Merge Patch (RFC 7386): updating email, deleting nickname with null, leaving all other fields untouched.

Common mistakes

Mistake

Sending a partial object with PUT to update one field.

Do this instead

PUT replaces the whole resource, so omitted fields get wiped or reset. Use PATCH for partial updates, or send the complete object with PUT.

Mistake

Assuming every PATCH request is idempotent and safe to blindly retry.

Do this instead

PATCH is not guaranteed idempotent — array-append or increment operations repeat their effect on retry. Design the patch to be idempotent or guard retries with an idempotency key.

Interview questions

Quiz

Question 1 of 30 ✓
What is the core difference between PUT and PATCH?
Select an answer to continue.

Flashcards

Card 1 of 3

Show answer · Space

Summary

PUT replaces an entire resource with a full representation and is idempotent, so it is safe to retry. PATCH applies a partial modification via a patch document — JSON Merge Patch (RFC 7386) or JSON Patch (RFC 6902) — and is not guaranteed idempotent. Use PUT to overwrite the whole thing (and it may create the resource, returning 201); use PATCH to change specific fields (returning 200 with a body or 204 without).

References

  • MDN — PATCH request method
  • RFC 7386 — JSON Merge Patch

Related topics

HTTP MethodsREST API Fundamentals
Previous
REST API Fundamentals

On this page

  • Learning objectives
  • Simple explanation
  • Technical explanation
  • Why it matters
  • How it works
  • Real-world analogy
  • Important terminology
  • Key definitions
  • Must memorize
  • Real-world example
  • Code example
  • Common mistakes
  • Interview questions
  • Quiz
  • Flashcards
  • Summary