QA Stacks

When to use PUT & When to use Patch...?

When to use PUT & When to use Patch?

Ans: Let’s assume the below user details for an example:

{
"id": 1,
"name": "Sam Kwee",
"email": "saket@newdomain.com", // the email changed, yay! "address": "123 Mockingbird Lane",
"city": "New York",
"state": "NY",
"zip": "10001"
}

Let’s suppose you want to do some modification and you want to remove a field like ‘address’ Now you can include all of the parameters which you want as result set. So here you will update the new values for the fields which you want to update and we won’t include ‘address’ in our new PUT request payload. Now you will get the result like below:

{
"id": 1,
"name": "Sam Kwee",
"email": "Ajay@newdomain.com”, // the email changed, yay! "city": "Washington”, // the City changed
"state": "NY",
"zip": "10003"    // zip code changed
}
And we will use PATCH method when we want to modification in ‘City’ field. PATCH /users/1
{"city": "Vegas"}

Now result set would be like below:
 

{"id": 1,
"name": "Sam Kwee",
"email": "saket@newdomain.com", "address": "123 Mockingbird Lane", "city": "Vegas”,    // City changed "state": "NY",
"zip": "10001"
}

NOTE: So basically, PUT is overriding/ Replacing the existing entity and PATCH is modifying the existing entity.