PUT/PATCH REST APIs - All you need to know about why and where to use Put vs Patch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to sudo boot in case you guys don't know me and you're new to the channel my name is yokita sharma and i work as a software engineer for curry this is a video in the series of rest api videos and today we are going to discuss put and patch methods so let's get started [Music] up until now we have seen how to use post methods to create a resource how to use get method to fetch those resources now we want to update those resources here comes comes into the picture put and patch request now you might have heard multiple times that put requests are used to only update resources and that is partially true put request can be used in order to update an existing resource or put request can also be used to create a resource sometimes this whole operation is known as absence absurd means if the resource does not exist create the resource or if the resource exists then update the resource the important thing to note here is whenever you are using a put request you have to send the whole body of the resource in the request payload similar to what we have seen in the post method along with the request uri we send the resource body in the request payroll similarly in case of put request also you have to send the whole body now the question comes whether put can be used to create or update how do you know that very simple if you want to do an absurd it can happen only when the client who is sending the request has access to the id of the resource either they know the id of the resource or they are able to generate the id on their own and the server supports the id sent from the client so let's see this is the uri that we will be using to update the resource the customers and in the request body we send customer id as one along with the other attributes of customer right as soon as we send this request the server sees the request and realizes that customer id customer with id1 already exist in this case the server is going to update the whole customer resource with the attributes that we have just sent in the put request and in response the server is going to return the whole object itself with the response code as 200 so in this case as you see an absurd operation has happened since the resource already existed a new resource has not been created in fact the same resource has been updated now let's see what happens when the resource doesn't exist on the server side in this request as you can see we are sending customer id 100 along with the customer attributes as soon as this request is sent the server realizes that it doesn't have any customer with id100 and according to the implementation server trust on the id sent from the uh client for for put request so what the server is going to do since any customer resource with id100 doesn't exist it is going to create this resource this is an absurd operation because if the resource doesn't exist create the resource if the resource exists update the resource which is exactly what has happened in these last two cases and as far as the responses goes for these two apis or for these two calls the response is going to be the whole customer object this is how as you can see put can be used to create as well as updated resource now next case let's say that the server doesn't support upset operation and server doesn't trust on the id sent by the put request it in that case it can only update the existing resource in the case when the server supports absurd operation or allows put to create resources we are expecting the id of the resource to be sent in the body and not as the path parameter why because if the absurd operation has to be supported and if you send the id in path parameter in the case where the resource is not existing it will result in a 404 and that's why we send the id in the request body so this was about supporting observed operation using put method now let's see if the server implementation does not support absurd using foot operation it only supports update of the resources in this case note that we are going to send a request with the id of the customer in path parameter and the whole customer object in the body of the request payload of put request so as you can see this is the path parameter and this is the body and now we are going to send the request what will happen on the server side is looking at the id which exists in the server side the whole body the of the resource that is saved in the server side is going to be replaced with the body that has been sent with put request all the attributes will be replaced let's say there were six attributes before before put request was sent and in the new put request you have omitted phone number and once you send this request what will happen the phone number entry that that existed earlier in the database will be removed now because the new put request is sent empty obviously this will happen if we don't have any validation on the phone number field but the point i am trying to make here is whatever you sent in the put request completely replaces what is present on the back end so this is how you can use put request for absurd and just update operations coming to the errors of put request obviously if you're going to use a path parameter uh an id in the path parameter which doesn't uh exist you're going to get a 404 in response and if you're going to use just like we have seen in the post if you're going to send any attribute which is invalid like empty phone number or string for your phone number or invalid email id you will get the same errors as you have received in the case of post you will get 400 band request and you will get the errors that you have sent invalid email address or invalid phone number etc now can you get 409 in case of put no because definitely you're going to use the id which already exists on the server side and you're trying to update it so you will not get 409 in case of footprints so that was all about foot method now coming on to patch what is the difference between put in patch as you have seen we just understood that in case of post request the whole body of resource is replaced by whatever attributes are sent in put request now let's say you don't want to replace the whole body you you just want to modify one or two attributes of a resource in that case you can use patch as the name suggest it is just patching or changing few things in the existing resource so let's say you just want to update the city of a customer you will just use the same uri as you are using input you will use the method patch and we will just send the changes that you want to apply to the resource let's say the current country of the user is bangalore and now you want to update the city to dubai you will not send the whole customer body in the payload you will just send city dubai that means you are only sending the changes or the difference that you want to apply and you will use patch request for the same this will just go and update only that field of the resource on the server side so this is the main use case of patch however if you are still sending all of the attributes in the patch request itself you might as well use put and let patch go and don't use patch but if you have if you have a use case of updating one or two fields or applying some changes in a resource then patch is the best way to go about now you might be thinking that if the we can do same thing using put why there is a need for sending using patch request or why patch patch method even exist then think about this there are some resources which will have hundreds of things in that case it will it might become very cumbersome for the client to send the whole body again and again and it will also slow down the request because you're transferring a lot of data instead if you just want to update one or two fields and you don't have to send all the 100 attributes just sending two attributes will actually make your apis faster and efficient so that is why patch methods are used now talking about item potency both put and patch request are supposed to be idempotent why because once you update a resource using a put method second time also if you send the same request the server state is not going to change as we have discussed the property of item potency is that your request is not going to change the state of the server no matter how many times you send that request this stands true for put as well as patch as well as get also the only uh method which should not be item potent or which should not support item potency is the post method similar to how we have uh fetched orders for customer or created orders for customer similarly we can update uh the orders for customer as well in this case we will just say customer slash one slash order and the order id and we are going to update the order id i am just covering this so that we cover both customer and order resources in all the videos this was a brief and slightly detailed introduction to put and patch methods i have included all the code and different resources and all the links are in the description if i have missed anything or if you wanted to learn something that i have not covered please feel free to put that in comments so that i can include it in upcoming videos if you have any questions any queries about this video please let me know in comments for those as well till then take care see you in the next video [Music] you
Info
Channel: sudoCODE
Views: 76,238
Rating: undefined out of 5
Keywords: put method, put http, put http method, put api, put apis, put api explained, patch http method, patch method, patch api method, what is the difference between put and patch, put vs patch, when to use put and patch, differences between put and patch, rest, rest api, rest apis, rest api course, sudocode, yogita sharma rest api, rest api primer course, understand put method, can put be used as post, post vs put, post vs patch
Id: LJajkjI5RHE
Channel Id: undefined
Length: 9min 30sec (570 seconds)
Published: Wed Sep 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.