REST APIs can be used to perform different actions. Based on the actions, the relevant method should be used. The following are the 5 methods supported by REST.
GET - This method is used to retrieve a data from database / server.
POST - This method is used to create a new record.
PUT - This method is used to modify / replace the record. It replaces the entire record.
PATCH - This method is used to modify / update the record. It replaces parts of the record.
DELETE - This method is used to delete the record.
Let us see this with an example. We know that mothers never get enough rest. But lets take these rest-less mommies as an example and see how they use Rest API. :)
Amongst the plethora of demands by a newborn, diapering cements its place in the leaderboard.
A mother wants everything best for the baby. So, its obvious that the mother would want to choose the best diaper for her baby. So, she goes to a shopping website(assume: amazon) and searches for diapers. This will send a HTTP Request to amazon’s server to GET the list of all diapers. Amazon’s server responds with a HTTP Response which will be a JSON object(assume) containing a list of diapers with some basic details. Amazon’s website reads this Response and converts to human readable format and displays it on the webpage for the customer aka. mother to see it.
After performing several GETs, ie, browsing through several diapers, she chooses a particular diaper for her newborn baby and adds it to her list. This creates a POST request where a new record is created in amazon’s database containing the diaper brand, size, quantity, price etc.
Her baby keeps growing and soon outgrows the newborn size. Suppose, the mother still likes the diaper and just wants to size up, all she has to do is just choose the new diaper size. When she updates the diaper size from size newborn to size 1, this triggers a PATCH method where everything else remains same and only the size of the diaper is changed.
It is very common for the mother to not be delighted with the current brand and decide to switch to an alternate the brand. Here, the mother will initiate a PUT request where the entire data containing previously chosen brand is modified and replaced with data corresponding to the newly chosen brand.
Finally, after a series of experiments involving several GETs, POSTs, PUTs and PATCHs, it's time for the mother to potty train the child. If she succeeds in training the child(Hurray!!) , then the diapers will no longer be required. This triggers a DELETE request.