Rajaram Shelar
Rajaram Shelar

Reputation: 7877

Actual use of GET,PUT,DELETE,POST methods in http

I am using asp.net web API(RESTful service) where I can use post method for delete, insert and update operation by giving some extra parameters. Is it used for only semantic usage of the words that can differentiate functionality?.

I found nice SO post Here. There are some doubts as below

  1. I can use POST for posting data then why PUT
  2. I can delete by POST by passing deleted id then why DELETE
  3. What happens at server side when it receives such header methods for each.

Please assist for the same. Thanks..

Upvotes: 1

Views: 2313

Answers (2)

Guanxi
Guanxi

Reputation: 3131

One of the Architecture properties of REST Architecture is "Simplicity of Interface". GET, POST, PUT etc. are well known verbs of HTTP that were available earlier as well. REST only defines how these can be used to achieve RESTFUL implementation. If you follow them you are doing RESTful implementation, if not .. you are doing something that only you know.

You can make use of GET as well to delete a record (by just providing ID in query string and writing underlying implementation to delete whatever ID is supplied in QS) but how will a person outside of your dream world know that if I do that with GET, the record will be deleted.

All the verbs are assigned a purpose by REST Architecture so that everybody knows what is going to happen. If you don't follow them, you are creating something that is not standard.

If you go by definition of REST (from wiki):

Representational state transfer (REST) is a software architectural style consisting of a coordinated set of architectural constraints applied to components, connectors, and data elements, within a distributed hypermedia system. REST ignores the details of component implementation and protocol syntax in order to focus on the roles of components, the constraints upon their interaction with other components, and their interpretation of significant data elements.

Upvotes: 3

A H K
A H K

Reputation: 1750

There are four basic methods in HTTP: GET, POST, PUT, and DELETE. GET is used most of the time. It is used for anything that's safe, that doesn't cause any side effects. GET is able to be bookmarked, cached, linked to, passed through a proxy server. It is a very powerful operation, a very useful operation.

POST by contrast is perhaps the most powerful operation. It can do anything. There are no limits as to what can happen, and as a result, you have to be very careful with it. You don't bookmark it. You don't cache it. You don't pre-fetch it. You don't do anything with a POST without asking the user. Do you want to do this? If the user presses the button, you can POST some content. But you're not going to look at all the buttons on a page, and start randomly pressing them. By contrast browsers might look at all the links on the page and pre-fetch them, or pre-fetch the ones they think are most likely to be followed next. And in fact some browsers and Firefox extensions and various other tools have tried to do that at one point or another.

PUT and DELETE are in the middle between GET and POST. The difference between PUT or DELETE and POST is that PUT and DELETE are idempotent, whereas POST is not. PUT and DELETE can be repeated if necessary. Let's say you're trying to upload a new page to a site. Say you want to create a new page at http://www.example.com/foo.html, so you type your content and you PUT it at that URL. The server creates that page at that URL that you supply. Now, let's suppose for some reason your network connection goes down. You aren't sure, did the request get through or not? Maybe the network is slow. Maybe there was a proxy server problem. So it's perfectly OK to try it again, or again—as many times as you like. Because PUTTING the same document to the same URL ten times won't be any different than putting it once. The same is true for DELETE. You can DELETE something ten times, and that's the same as deleting it once.

By contrast, POST, may cause something different to happen each time. Imagine you are checking out of an online store by pressing the buy button. If you send that POST request again, you could end up buying everything in your cart a second time. If you send it again, you've bought it a third time. That's why browsers have to be very careful about repeating POST operations without explicit user consent, because POST may cause two things to happen if you do it twice, three things if you do it three times. With PUT and DELETE, there's a big difference between zero requests and one, but there's no difference between one request and ten.

Example

POST:

Used to modify and update a resource

POST /questions/ HTTP/1.1 Host: wahteverblahblah.com Note that the following is an error:

POST /questions/<new_question> HTTP/1.1
Host: wahteverblahblah.com

If the URL is not yet created, you should not be using POST to create it while specifying the name. This should result in a 'resource not found' error because does not exist yet. You should PUT the resource on the server first.

You could though do something like this to create a resources using POST:

POST /questions HTTP/1.1
Host: wahteverblahblah.com

Note that in this case the resource name is not specified, the new objects URL path would be returned to you.

PUT:

Used to create a resource, or overwrite it. While you specify the resources new URL.

For a new resource:

PUT /questions/<new_question> HTTP/1.1
Host: wahteverblahblah.com
To overwrite an existing resource:

PUT /questions/<existing_question> HTTP/1.1
Host: wahteverblahblah.com

Also have a look at http://jcalcote.wordpress.com/2008/10/16/put-or-post-the-rest-of-the-story/

Upvotes: 2

Related Questions