Understanding the Difference Between PUT and PATCH in REST APIs

Ritik Chourasiya
3 min readJul 28, 2024

--

Difference Between PUT and PATCH in REST APIs

Today, I want to delve into a topic that often comes up when working with REST APIs: the difference between PUT and PATCH. As a software engineer, I frequently use both methods, and understanding their distinct roles can make a big difference in how we design and interact with our APIs. So, let’s break it down!

The Full Replacement Power of PUT

First up, let’s talk about PUT. Think of PUT like giving your API a completely new blueprint for a resource. When you send a PUT request, you're essentially saying, "Here's the new version of the resource. Replace everything you have with this."

Here’s a little example to illustrate:

PUT /users/1
Content-Type: application/json
{
"id": 1,
"name": "John Doe",
"email": "john.doe@example.com",
"age": 30
}

In this request, I’m updating the user with ID 1, providing the complete set of user details. If any fields are missing in the request, they get reset to their default values. It’s like redecorating your entire room instead of just changing the curtains.

In this request, I’m updating the user with ID 1, providing the complete set of user details. If any fields are missing in the request, they get reset to their default values. It’s like redecorating your entire room instead of just changing the curtains.

One key thing to remember is that PUT is idempotent. This fancy word means that no matter how many times you send the same PUT request, the result will always be the same. Imagine repainting your room the same color over and over again – it looks the same after each coat.

The Precision of PATCH

On the other hand, PATCH is more like a scalpel than a sledgehammer. With PATCH, you’re making precise updates to specific parts of a resource without touching the rest. It’s perfect for those times when you just want to change the curtains without redecorating the entire room.

Here’s an example of a PATCH request:

PATCH /users/1
Content-Type: application/json

{
"age": 31
}

In this case, I’m only updating the age of the user with ID 1. The rest of the user’s information remains unchanged. It’s a quick and efficient way to make small updates.

PATCH can be idempotent, but it’s not a guarantee. The result can vary based on the current state of the resource. Imagine adjusting the brightness of a lamp in a room; depending on the current brightness, the effect of your action might be different each time.

When to Use What

So, when should you use PUT versus PATCH? Here’s my rule of thumb:

  • Use PUT when you want to update a resource entirely. It’s like starting fresh with a new blueprint.
  • Use PATCH when you only need to make small, specific updates. It’s like making tweaks without a full overhaul.

Understanding these differences not only helps in designing better APIs but also ensures that your applications are efficient and maintainable.

I hope this clears up the confusion between PUT and PATCH. Feel free to share your thoughts or any experiences you’ve had with these methods. Happy coding!

--

--

Ritik Chourasiya
Ritik Chourasiya

Written by Ritik Chourasiya

I’m a 22 year old, still undergraduate backend developer based in India, with 2 years of experience in the software development industry.

No responses yet