6

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)?

Esteban Küber
  • 36,388
  • 15
  • 79
  • 97
Barbaros Alp
  • 6,405
  • 8
  • 47
  • 61
  • 1
    Dupe of http://stackoverflow.com/questions/343288/what-am-i-not-understanding-about-rest http://stackoverflow.com/questions/941016/what-is-rest-closed – Esteban Küber Mar 22 '10 at 15:46
  • i can not understand the real life practical use either. What is an actual scenario to use those verbs that you can not achive with get or post. i think it is rather an philosophical aproach rather than practical usage. – Add080bbA Jan 06 '15 at 18:23

5 Answers5

13
  • 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.

Anthony Walsh
  • 492
  • 1
  • 6
  • 14
Esteban Küber
  • 36,388
  • 15
  • 79
  • 97
  • 1
    And by UPDATE, do you mean PUT? http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html –  Jul 11 '11 at 16:02
  • 3
    The word often used is idempotent: repeating the action multiple times results in the same change. GET, PUT and DELETE are all idempotent actions. GET is also safe (it doesn't make any changes). DELETE is idempotent, as deleting the same resource twice still deletes it (although the second request may not 'succeed'. POST requests are not guaranteed to be idempotent, but depending upon the system they may be. If a POST is used for an update, then it may very well be. – Matthew Schinckel Apr 10 '12 at 22:29
4

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.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • Thanks, this is already what i know but do they have any advantages rather than using a POST verb for removing data, for example – Barbaros Alp Mar 22 '10 at 15:45
  • POST sends data to code running on a webserver (most commonly, code that generates a webpage). PUT sends data to the webserver software itself. – Chris S Mar 22 '10 at 15:46
  • 2
    There can be only one Chris S here! – Chris S Mar 22 '10 at 15:48
  • One advantage is that the server-side framework can use the fact that it is a PUT or DELETE request to route the request automatically to the proper controller method, instead of having to specify this in the URL. However, I am not sure if ASP.NET-MVC does this... – Justin Ethier Mar 22 '10 at 15:48
2

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

Brian
  • 4,974
  • 2
  • 28
  • 30
0

First, you should check out BlaM's very good answer to this (dupe?) question.

Obviously you can technically create/update/delete resources without using REST principles, ut you're missing a point. If you still don't really get the concepts behind REST, Ryan Tomayko's blog entry is a nice place to start.

Community
  • 1
  • 1
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
-3

The original purpose was to edit webpages using those verbs (more on the RESTful system). They have since been deprecated by the WebDAV extension. In practice, PUT and DELETE are never used (or very rarely by custom built applications).

Chris S
  • 766
  • 4
  • 13
  • Rarely? Not quite true today with all of those RESTful frameworks out there. And as far as I know they are not deprecated, the WebDAV extension just blocks them on windows for some reason. I am not downvoting you yet, I'm giving you a chance to clarify. – the_drow Oct 10 '10 at 16:20
  • @the_drow, Yes really. Considering the vast majority of web traffic never comes close to using PUT or DELETE, I'm comfortable in saying that it's very rarely used. The only applications I'm aware of that use PUT or DELETE are data abstraction layers that have nothing to do with browsers or hypertext transport. – Chris S Oct 11 '10 at 03:38
  • If you are claiming that they are deprecated, why on HTML5 they are added to the form's method attribute? Also you haven't refered to my comment about WebDAV, if it does deprecate PUT and DELETE it is not RESTful and due to that programmers who use asp.net mvc and hope to achieve disable it. – the_drow Oct 11 '10 at 07:40
  • 1
    @The_drow, the original PUT and DELETE commands were replaced in WebDAV. HTML5 is a document language specification with recommendations for the transport, not a communications protocol. I think you misunderstand what a RESTful system is, it does not inherently require PUT and DELETE verbs (either from the original spec, or the WebDAV spec). – Chris S Oct 11 '10 at 13:06
  • There should be a mean to create and delete then, if there isn't, WebDAV is not RESTful. That's what I meant. – the_drow Oct 12 '10 at 13:44