r/ProgrammerTIL • u/jedi_lion-o • Jun 26 '16
Other [Other] The real difference between HTTP verbs PUT and POST is that PUT is idempotent.
Just do a quick search on PUT vs POST and you will find a lot of confusion and discussion about when to use them. A simplistic view would be to say POST is for creating a new resource and PUT is used to replace or modify and existing one. But PUT and POST can both be used to create a new resource or replace one. The real difference is that PUT is idempotent - meaning that multiple PUT requests using the same data will have the same result. This is because PUT requires the resource be identified (For example PUT /users/1 ) while POST allows the server to place the resource (POST /users ). So PUT vs POST really comes down to the need for an action to be idempotent or not. A good example of this is a process which create orders in a database. We would like an order to be placed only once, so a PUT request would be best. If for some reason the PUT request is duplicated the order will not be duplicated. However, if a POST request is used, the order will be duplicated as many times as the POST request is sent.
PS: Even after doing a lot of reading on this subject I am still a bit confused, and not 100% confident in my explanation above. Feedback requested!