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
- Client sends only the updated fields
- Server merges incoming data with existing fields
- Only specified fields are changed
- Other fields remain untouched
- 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
agefield 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)
| Feature | PATCH | PUT |
|---|---|---|
| Purpose | Partial update | Full replacement |
| Efficiency | High | Medium |
| Data Sent | Only changed fields | Entire resource |
| Risk of overwriting | Low | High |
| Idempotence | Sometimes | Always |
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 Code | Meaning |
|---|---|
| 200 OK | Resource updated successfully |
| 204 No Content | Successful update, no body returned |
| 400 Bad Request | Invalid fields or syntax |
| 404 Not Found | Resource does not exist |
| 409 Conflict | Update conflicts with existing rules |
| 500 Internal Server Error | Server 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
