Difference Between PUT and PATCH Request (original) (raw)

HTTP PUT request is used to replace and update the entire resource or document, while the PATCH request only updates the specific parts of that document.

When working with APIs, figuring out the right way to update resources can be tricky. Both **PUT and **PATCH requests are used for this purpose. This guide will break down the key differences between these two methods, helping you choose the most appropriate approach for your needs.

Difference between PUT and PATCH requests

Here is a detailed comparison of PUT and PATCH based on various features:

Features PUT PATCH
Purpose Used to update or replace an entire resource. Used to apply partial modifications to a resource.
Data Handling Requires the client to send the complete resource representation. Requires only the changes (delta) to be sent, not the entire resource.
Error Handling If the resource doesn’t exist, it may create a new one (depending on implementation). Typically used only for existing resources; may fail if the resource doesn’t exist.
Performance It can be less efficient for large resources, as the entire resource is sent. More efficient for small changes, as only the necessary data is sent.
Request Body Contains the full resource representation. Contains only the fields or data to be updated.
Use Case Best for replacing a resource entirely (e.g., updating a user profile). Best for making small updates (e.g., changing a user's email address).
Example PUT /users/1 with full user data updates the entire user resource. PATCH /users/1 with { "email": "new@example.com" } updates only the email.

**PUT Request

A PUT request is used to update an entire resource on the server. When you use a PUT request, you are telling the server to completely replace the existing data with the new data you provide.

Key Points

  1. **Replaces Entire Resource: When you send a PUT request, the server expects you to include all the information for the resource, even if you only want to update a small part of it. If you leave something out, that part of the resource will be erased or set to default.
  2. **Can Create Resources: If the resource doesn’t exist, a PUT request can be created for it. This is possible because PUT specifies the exact URL where the resource should be created.

**Now, let us understand with the example

**Example: Let’s say you want to update a user’s name and email. With PUT, you would send all the details of the user, even those that aren’t changing:

PUT /users/123
{
"id": 123,
"name": "anjali",
"email": "123@example.com",
"age": 20
}

When you use a PUT request to update data on the server, you need to send all the data for that object, not just the fields you want to change. For example, if you're updating the name and email of a user, you must send the id, and age as well.

JavaScript ``

const express = require('express'); const app = express(); const port = 3000;

app.use(express.json()); // Define a route for PUT requests app.put('/users/:id', (req, res) => { const userId = req.params.id; const updatedUser = req.body; res.json({ message: User with ID ${userId} updated, updatedUser }); }); // Start the server app.listen(port, () => { console.log(Server is running on http://localhost:${port}); });

``

**Output

**In this example

**Patch Request

A **PATCH request is used to partially update a resource. This means you only need to send the data that you want to change, without affecting the rest of the resource.

Key Points

  1. **Partial Updates: With PATCH, you only include the fields you want to update. The rest of the resource remains unchanged.
  2. **More Efficient: Since you’re sending less data, PATCH requests usually use less bandwidth, making them faster and more efficient.

If you only want to update the user’s name and email, you would send just those fields:

{
"id":"123", // field that to be updated
"age":"40" // field that to be updated
}

In the example, we have made a PATCH request to the server, with a payload attached to the body. If we want to update the id and age, with a PATCH request then we have to send only the fields that have to be updated.

JavaScript ``

const express = require('express'); const app = express(); const port = 3000;

app.use(express.json()); // Define a route for PATCH requests (Partial update) app.patch('/users/:id', (req, res) => { const userId = req.params.id; const partialUpdate = req.body; res.json({ message: User with ID ${userId} updated, updatedUser });

}); // Start the server app.listen(port, () => { console.log(Server is running on http://localhost:${port}); });

``

**Output

**In this example

Which One Should You Use?

Use PUT When

Use PATCH When

Conclusion

When deciding between PUT and PATCH, consider whether you need to update the entire resource or just part of it. Use PUT when you want to completely replace a resource, and use PATCH when you only need to make small updates.