Open In App

Difference Between PUT and PATCH Request

Last Updated : 08 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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": "[email protected]" } 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": "[email protected]",
"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

  • An Express server is created and listens on port 3000, with middleware express.json() to handle incoming JSON data in the request body.
  • A sample user object is defined in an array users, which includes id, name, email, and age.
  • The /users/:id PUT route is defined to handle complete replacements of a user's data. The user is identified by id in the URL.
  • The server searches for the user with the provided id. If found, the existing user data is completely replaced by the new data from the request body, ensuring the id remains unchanged.
  • If the user is successfully updated, the updated data is sent back in a success message. If the user is not found, a 404 error message is returned.

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

  • The Express server is set up and listens on port 3000, using express.json() middleware to parse incoming JSON request bodies.
  • A sample list of users is created in memory with one user object containing id, name, email, and age.
  • The /users/:id PATCH route allows partial updates to a specific user's data. The user is identified by id passed in the URL.
  • The server searches for the user with the given id. If found, it merges the existing user data with the new data sent in the request body, updating only the fields that are provided.
  • If the user is successfully updated, a success message with the updated data is sent back. If no user is found, a 404 error message is returned.

Which One Should You Use?

Use PUT When

  • You need to replace the entire resource with new data.
  • You are creating a new resource (if the resource does not exist).
  • The data you are updating is complete, and replacing the whole resource is appropriate.

Use PATCH When

  • You only need to update specific fields of the resource.
  • The update is incremental or partial (e.g., changing a user’s email).
  • You want to minimize the amount of data sent over the network.

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.


Next Article

Similar Reads