Difference Between PUT and PATCH Request
Last Updated :
08 Mar, 2025
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
- 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.
- 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
- Partial Updates: With PATCH, you only include the fields you want to update. The rest of the resource remains unchanged.
- 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.
Similar Reads
Difference Between Git Fetch and Git Pull
Understanding the difference between git fetch and git pull is important for effective version control in Git. Git Fetch and Git Pull are two important commands in Git that help in managing remote repositories. While both commands involve retrieving data from remote repositories, they serve distinct
5 min read
Difference between PUT and DELETE request in Vanilla JavaScript
PUT and DELETE are the two different types of HTTP request methods. HTTP protocol supports many methods to transfer data from the server or do any operation on the server. The HTTP protocol supports the following methods, e.g. GET, POST, PUT, DELETE, PATCH, COPY, HEAD, OPTIONS, etc. Before we dive i
3 min read
Difference between HTTP GET and POST Methods
HTTP (Hypertext Transfer Protocol) specifies a collection of request methods to specify what action is to be performed on a particular resource. The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. This article covers the 2 most common HTTP request methods, i.e. the GET
4 min read
Difference between GET and POST request in Vanilla JavaScript
In this article, we will learn about the GET & POST request method in vanilla Javascript, & will also understand these 2 methods through the examples. GET and POST is two different types of HTTP request methods. HTTP protocol supports many methods to transfer data from the server or perform
3 min read
Difference Between "git commit" and "git push"?
Git commit and git push are two essential commands you'll use a lot when working with Git. even their frequent use together, they have different functions. In order to help you understand when and how to use these two commands effectively in your version control workflow, this article will break dow
2 min read
Difference Between REST API and RESTful API
Both REST API and RESTful API are often used interchangeably in the software development community, but there are subtle differences between the two. Understanding these differences is important for building modern web applications, as both have significant roles in enabling communication between cl
6 min read
Git - Difference Between Merging and Rebasing
When working with Git, two common strategies for integrating changes from different branches are merging and rebasing. Both techniques serve the purpose of combining code from multiple branches, but they do so in different ways. This article will help you understand the differences between merging a
3 min read
Difference between PUT and POST HTTP Request in Express and Postman
Both PUT and POST are request methods used in the HTTP protocol. In this article, we will introduce the HTTP methods such as PUT and POST in detail, and we will also discuss in detail when to use PUT and when to use POST. Table of Content PUT methodPOST MethodDifference between PUT and POST methodCo
5 min read
Difference between HTTP/2 and HTTP/1.1
HTTP stands for hypertext transfer protocol & it is used in client-server communication. By using HTTP user sends the request to the server & the server sends the response to the user. There are several stages of development of HTTP but we will focus mainly on HTTP/1.1 which was created in 1
3 min read
Difference Between REST API and RPC API
REST and RPC are design architectures widely used in web development to build APIs (Application Programming Interface). It is a set of instructions that permits two systems to share resources and services. The client creates a request to the server that responds to it with data in JSON or XML format
3 min read