PUT
and PATCH
are HTTP
methods used to update a resource. They are both for updating a resource, yet they differ in how they apply updates.
PUT
PUT
replaces the entire resource with new values, it overwrites all fields.
Behavior:
- If the resource exists, it is completely replaced with the new version, it getts overwritten.
- If a field is missing in the request, it gets removed.
Example
Imagine you have a user resource with this initial data:
{
"id": 123,
"firstName": "John",
"lastName": "Doe",
"age": 25,
"email": "[email protected]"
}
And I want to change the age and email. So, we send the following PUT request:
{
"age": 20,
"email": "[email protected]"
}
The updated resource will now be:
{
"age": 20,
"email": "[email protected]"
}
The id
, firstName
and lastName
fields got deleted. Since in our PUT request we included only age
and email
fields (since those are the only ones we need to update), those got overwritten with new values, while the missing ones got removed.
PATCH
PATCH
method updates only the specified fields of a resource without touching the others.
Behavior:
- Only the provided fields are updated.
- Missing fields remain unchanged.
As opposed to PUT
method, this one doesn't remove the missing fields, just updates the specified ones, whereas PUT
overwrites the specified and removes missing ones.
Example
Let's take the same example as before, intial data:
{
"id": 123,
"firstName": "John",
"lastName": "Doe",
"age": 25,
"email": "[email protected]"
}
The same request, only this time we're using PATCH
method:
{
"age": 20,
"email": "[email protected]"
}
The result is:
{
"id": 123,
"firstName": "John",
"lastName": "Doe",
"age": 20,
"email": "[email protected]"
}
PATCH
method updated the specified fields with new values without touching other fields that weren't specified in the request, whereas PUT
would've deleted those.
PATCH vs PUT
When updating data via an API, should you use PUT
or PATCH
? While both modify resources, they work differently.
-
PUT
replaces the entire resource, removing unspecified fields -
PATCH
updates only specific fields, leaving the rest untouched
If using PUT
, you're forced to send a request with full resource, otherwise unused ones will be removed.
PATCH
is usually safer since it won’t accidentally remove existing fields.
When to use which?
PUT
:
- Updating an entire object: If you're sending a complete update (e.g., replacing an entire profile).
- Replacing an existing resource: If you expect the resource to be fully refreshed.
- Ensuring consistency: If you want a predictable, clean state.
PATCH
:
- Making minor changes: If you only need to update one or a few fields.
- Preserving existing data: When you don’t want to overwrite fields unnecessarily.
- Reducing bandwidth usage: If you want a more efficient request.
Summary
Feature | PUT | PATCH |
---|---|---|
Update Type | Full replacement | Partial update |
Fields required? | Yes, all fields must be sent | No, only changed fields are sent |
If a field is missing? | It gets removed | It stays unchanged |
Data Size | Larger (sends full resource) | Smaller (sends only changes) |
Best Use Case | When you want to replace the entire resource | When you only need to update specific fields |