![]() |
VOOZH | about |
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.
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. |
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.
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.
Output
In this example
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.
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.
Output
In this example
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.