PUT vs PATCH difference

Finally Jackson has started building his APIs, and also few of them are in Production. After some time, his scrum master comes up with a new requirement of one of his API. Let’s say he build an API for an airport to get information about all the flights originating from that airport. He is sending a JSON response to the frontend folks which is a simple JSON with some 5-6 fields on it, such as the airline provider, it’s bay number, the carrier capacity, the airplane manufacturer, the model number etc. This API is exceptionally running well in real life with no issues at all, and Jackson is chilling at his home office just watching the transactions (with some popcorns? or just some coffee). All is going when until he is told that there may be a change in few parameters of the airplane, like the Bay area is most likely to change. How do he incorporate this? Do he need a new API for this? Let’s find out.

Existing APIs

Let’s quickly take a look at the existing APIs that he has. So he has a Create request to put the data of a new airplane. He has a retrieval request to know about any plane with the unique id and some filters. There are just POST method and GET method for the time being. This is because the manager had no plans of changing the Bay area, so there might be no need for an update for possibly few months.

POST/GET
https://terminalhyatt/carriers/flights/v1/(flightNumber)

Possible solutions

Jackson decided to go with the PUT method as it is an update which is required. And PUT method will definitely serve the business requirements and also not technically incorrect to use in this case. And he was very happy until his architect told him to check for the PATCH method before using the POST. This made him concerned and his happiness meter was soon on the lower side. As it was a Friday and he was very happy on implementing the POST method on his APIs. Now he is sitting and looking for the PATCH method and it’s advantages. After few research, he had a discussion with the business team. And the business team confirmed that at least for the next 2 years, the only parameter which will possibly change is the Bay area, and there shall be no change in the other parameters. This information was enough for Jackson to go with the PATCH method instead of POST. Why only PATCH and why can’t POST? and How they are different?

PUT/PATCH
https://terminalhyatt/carriers/flights/v1/(flightNumber)

Differences between the PUT and PATCH method

Though PUT method is being used since ages to update the requests and is very successful in its operation also. But the PUT method is idempotent and requires the whole request body to be give, even if there is one change. There might be variations by giving the mandatory and the optional fields. But if there are multiple parameters, you simply can’t keep flipping the mandatory ones as and when new requirements comes in. That’s when the PATCH method comes handy. This method only requires a fraction of fields ranging from one to all. You might end up updating just one parameter, with no limitation on the semantics matching with the application. On the other hand, the semantics has to be matched with that in the application. So, you not only use less bandwidth, but you also makes the life of the API developers easier. As they can simply put the field which needs to be changed and submit the PATCH request. (Please note that the example quoted above talks about the change of one parameter, but PATCH request can be used if the fraction of the parameters are being changed, and is not limited to just one.)

The difference between a PUT and PATCH method is similar to what we use in literal English. When you want to paint the walls of your room, and you want to change the color of the paint, you will PUT the paint on the walls. When you want to paint one of the four walls, in that case, you will either PUT the paint or just PATCHfully paint that wall. On the other hand, if there is a damage in a fraction of one of the walls, and you want to paint it. You will apply a PATCH of paint to make it even.

Mrin

There are other scenarios also, if the body that needs to be changed is usually bigger than the existing one, you may go with the POST method as well. But let’s not get confused here. 99% of the times, the body to be updated will be either equal to the existing one (PUT method preferred) or will be a fraction of the existing one (PATCH method preferred).

I hope that from next time onwards you won’t find yourself in a situation where you will have to worry about the PUT method or the PATCH method.

One thought on “PUT vs PATCH difference

Leave a comment