savanka

PATCH Request Method Explained

Introduction

The PATCH method is one of the most commonly used HTTP request methods when you want to partially update a resource. Unlike PUT—which replaces an entire object—PATCH allows you to send only the fields you want to modify.

This makes PATCH more efficient, faster, and safer for incremental updates in modern REST APIs.


What Is a PATCH Request?

A PATCH request applies partial modifications to a resource at a specific URL.

Key Characteristics

  • Partially updates a resource
  • Not necessarily idempotent (depends on implementation)
  • Only the changed fields are sent in the request body
  • More efficient than PUT
  • Frequently used in REST APIs and mobile apps

When Should You Use PATCH?

Use PATCH when:

  • Updating a single field (e.g., username)
  • Changing a few properties (e.g., price and stock of a product)
  • Modifying only the necessary data
  • Reducing bandwidth usage
  • Updating settings or toggles (e.g., dark mode ON/OFF)

Examples where PATCH is ideal:

  • Changing one detail in a user profile
  • Updating a single product attribute in an inventory
  • Modifying a setting without affecting others

How PATCH Works Internally

  1. Client sends only the updated fields
  2. Server merges incoming data with existing fields
  3. Only specified fields are changed
  4. Other fields remain untouched
  5. Server returns updated resource or confirmation

This prevents unintentional overwriting of unrelated fields.


Example 1: JavaScript Fetch API (PATCH Request)

fetch("https://api.example.com/user/101", {
  method: "PATCH",
  headers: {
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    age: 27
  })
})
.then(res => res.json())
.then(data => console.log(data));

Explanation

  • Only the age field is updated
  • Other fields like name, email, etc., remain unchanged

Example 2: Using PATCH with cURL

curl -X PATCH https://example.com/api/products/55 \
-H "Content-Type: application/json" \
-d '{"price": 82000}'

Use Case

Updating only the price of a product without altering its name, description, or stock.


Example 3: PATCH in Express.js (Node.js)

app.patch("/api/user/:id", (req, res) => {
  const id = req.params.id;
  const updates = req.body;

  // Merge existing data with changes
  database[id] = { ...database[id], ...updates };

  res.json({ message: "User updated", data: database[id] });
});

Explanation

  • Existing user data is merged
  • Only provided fields are updated
  • Prevents accidental data loss

Example 4: JSON Patch Format Example

Some APIs follow the RFC 6902 JSON Patch Standard, using operations like “replace”, “add”, “remove”.

[
  { "op": "replace", "path": "/email", "value": "newemail@example.com" }
]

Meaning

  • Replace the email field with the new value

This structured format is useful in enterprise-level APIs.


PATCH vs PUT (Clear Comparison)

FeaturePATCHPUT
PurposePartial updateFull replacement
EfficiencyHighMedium
Data SentOnly changed fieldsEntire resource
Risk of overwritingLowHigh
IdempotenceSometimesAlways

When NOT to Use PATCH

Avoid PATCH when:

  • You need to replace or rewrite the entire object
  • The update is large
  • Your API or backend does not support merging logic

In such cases, PUT is more appropriate.


Common Server Response Codes for PATCH

Status CodeMeaning
200 OKResource updated successfully
204 No ContentSuccessful update, no body returned
400 Bad RequestInvalid fields or syntax
404 Not FoundResource does not exist
409 ConflictUpdate conflicts with existing rules
500 Internal Server ErrorServer failed to merge changes

Advantages of PATCH

  • Very efficient
  • No need to send entire object
  • Avoids unintentional wiping of fields
  • Ideal for incremental updates
  • Saves bandwidth on mobile devices and slow networks

Disadvantages of PATCH

  • More complex to implement on backend
  • Not always idempotent
  • Requires careful validation
Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *