Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need PUT or DELETE Http Verbs?

After the release of MVC 2, I have started to check and play with the new features, but i couldn't understand what is the use of PUT and DELETE verbs.

I have searched about it and read some articles but I couldn't get it.

What is the main purpose of DELETE and PUT? Do they have any advantages over using a GET or POST method instead (even though I can handle all of the requests with GET and POST)?

like image 718
Barbaros Alp Avatar asked Mar 22 '10 15:03

Barbaros Alp


3 Answers

Basically it's used to better distinguish actions/privileges.

Idempotent methods and web applications

Methods PUT and DELETE are defined to be idempotent, meaning that multiple identical requests should have the same effect as a single request. Methods GET, HEAD, OPTIONS and TRACE, being prescribed as safe, should also be idempotent, as HTTP is a stateless protocol. By contrast, the POST method is not necessarily idempotent, and therefore sending an identical POST request multiple times may further affect state or cause further side effects (such as financial transactions). In some cases this may be desirable, but in other cases this could be due to an accident, such as when a user does not realize that their action will result in sending another request, or they did not receive adequate feedback that their first request was successful. While web browsers may show alert dialog boxes to warn users in some cases where reloading a page may re-submit a POST request, it is generally up to the web application to handle cases where a POST request should not be submitted more than once. Note that whether a method is idempotent is not enforced by the protocol or web server. It is perfectly possible to write a web application in which (for example) a database insert or other non-idempotent action is triggered by a GET or other request. Ignoring this recommendation, however, may result in undesirable consequences if a user agent assumes that repeating the same request is safe when it isn't.

via wikipedia
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods

like image 45
used2could Avatar answered Sep 22 '22 10:09

used2could


  • GET: Only function is to send information back to the client. It should be a repeatable operation without side effects.

  • POST: It does operations with side effects. It is not repeatable (if you POST twice, the server acts twice). After operation it should redirect to another page to show the results using GET.

  • DELETE: Its only function is to do a destructive operation, not repeatable (once the object is deleted, there is nothing else to delete).

  • PUT: Its function is to modify a single object and update it with the values sent in a POST (like) way. Repeatable.

You can fake DELETE and PUT with POST (as some web browsers don't recognize DELETE and PUT).

Please, use GET only to display information, not for operations with side effects.

like image 181
Esteban Küber Avatar answered Sep 22 '22 10:09

Esteban Küber


In a RESTful architecture, DELETE is supposed to be used for requests that will remove data, and PUT is supposed to be used for requests that will insert data.

like image 39
Justin Ethier Avatar answered Sep 20 '22 10:09

Justin Ethier