PUT vs PATCH?
I’m sure by now we are all a little tired of talking about CRUD. But we are going to go back one more time just to hash out a minor detail we brushed past. We now know after learning sinatra and rails that our HTTP verbs had to match our CRUD actions.
- POST -> Create
- GET -> Read
- PATCH/PUT -> Update
- DELETE -> Delete
I’m sure we all noticed there were two options to update and we were told to use patch, but is there a difference? And does one make sense in different applications?
The main difference between PUT and PATCH is how the server processes the request to modify the resource identified by its Request URI. A PUT request tells the server to override the resource that is stored and to replace it with the incoming resource. A PATCH request tells the server that the resource stored needs to be modified to create a new version.
For example let’s say a business needs to update their customer information on their server, the resource that contains the contact information for someone might look something like this:
{
first_name: “Randy”
last_name: “Smith”
phone_number: “555–666–7777”
address: “292 Lincoln Place, Brooklyn, NY 11238”
}
With a PUT request we must make sure that if we want to update our contacts we must include all of the relevant information every single time we want to update our server. But with a PATCH request we can send only the pieces we need to update and not worry about the other information.
Updating this contact with a PUT would have to look like this:
{
first_name: “Randy”
last_name: “Smith”
phone_number: “111–111–1111”
address: “292 Lincoln Place, Brooklyn, NY 11238”
}
If we tried to just update the phone number in a PUT request we would end up with our server having a contact that only contained a phone number with no name attached. That would be bad for the company and sad for Randy.
Updating this contact with a PATCH can be more flexible:
{
phone_number: “111–111–1111”
}
This modifies the existing resource on the server and leaves the other information untouched.
Another thing to note is that PUT will attempt to modify an existing resource and, if it doesn’t exist, it will create a new resource. Calling PATCH on a resource that doesn’t exist will fail, and nothing will be created.
The last tidbit is that PUT is required to be idempotent. This assumes that PUT is referring to a resource and the resource is complete (the entire resource). For us this is really just saying that if we invoke PUT N times, the very first time will update our resource, then the rest of the N-1 requests will overwrite the same resource again and again. Effectively not changing the resource after the first PUT.
PUT being idempotent is important because the request can be repeated automatically if a communication failure occurs before the client is able to read the server’s response. If a client sends a PUT request and the connection is closed before any response is received then the client can establish a new connection and retry the request knowing that repeating the request will have the same intended effect as the original request. Whereas PATCH is not always idempotent such as this example:
We go to update Randy’s phone number and we send a patch just like the one above. Now this time the request fails because of a network issue and will be resubmitted later. In the meantime another coworker at a different branch accidentally updates Randy’s address incorrectly. When your initial request does finally go through, you are stuck with an incorrect address. Whereas if you used PUT and included all the relevant information, then when your request went through you would overwrite the bad address and have the correct contact info for Randy.
Definitions:
URI - stands for Uniform Resource Identifier, and it is the syntax used to help people identify your particular resource
Idempotent - refers to the property of certain operations where they can be applied multiple times without changing the result beyond the initial application
Resources:
https://en.wikipedia.org/wiki/Patch_verb
https://en.wikipedia.org/wiki/Idempotence
https://stackoverflow.com/questions/28459418/rest-api-put-vs-patch-with-real-life-examples
https://rapidapi.com/blog/put-vs-patch/
https://medium.com/backticks-tildes/restful-api-design-put-vs-patch-4a061aa3ed0b