Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is PUT/DELETE idempotent with REST automatic?

Tags:

rest

http

I am learning about REST and PUT/DELETE, I have read that both of those (along with GET) is idempotent meaning that multiple requests put the server into the same state.

Does a duplicate PUT/DELETE request ever leave the web browser (when using XMLHttpRequest)? In other words, will the server be updating the same database record for each PUT request, or will duplicate requests be ignored automatically?

If yes, how is using PUT or DELETE different from just using POST?

I read an article which suggested that RESTful web services were the way forward. Is there any particular reason why HTML5 forms do not support PUT/DELETE methods?

like image 291
Lea Hayes Avatar asked Aug 10 '11 19:08

Lea Hayes


People also ask

Is REST put idempotent?

One of the important aspects of REST (or at least HTTP) is the concept that some operations (verbs) are idempotent. As Gregor Roth said several years ago: The PUT method is idempotent. An idempotent method means that the result of a successful performed request is independent of the number of times it is executed.

IS PUT request idempotent?

The difference between PUT and POST is that PUT is idempotent: calling it once or several times successively has the same effect (that is no side effect), whereas successive identical POST requests may have additional effects, akin to placing an order several times.

Why is REST put idempotent?

From a RESTful service standpoint, for an operation (or service call) to be idempotent, clients can make that same call repeatedly while producing the same result. In other words, making multiple identical requests has the same effect as making a single request.

What HTTP methods are idempotent?

Idempotent HTTP methods Idempotency means that multiple identical requests will have the same outcome. So it does not matter if a request is sent once or multiple times. The following HTTP methods are idempotent: GET, HEAD, OPTIONS, TRACE, PUT and DELETE.


1 Answers

REST is just a design structure for data access and manipulation. There's no set-in-stone rules for how a server must react to data requests.

That being said, typically a REST request of PUT or DELETE would be as follows:

DELETE /item/10293 

or

PUT /item/23848 foo=bar fizz=buzz herp=derp 

The requests given are associated with a specific ID. Because of this, telling the server to delete the same ID 15 times will end up with pretty much the same result as calling it once, unless there's some sort of re-numbering going on.

With the PUT request, telling the server to update a specific item to specific values will also lead to the same result.

A case where a command would be non-idempotent would typically involve some sort of relative value:

DELETE /item/last 

Calling that 15 times would likely remove 15 items, rather than the same last item. An alternative using HTTP properly might look like:

POST /item/last?action=delete 

Again, REST isn't an official spec, it's just a structure with some common qualities. There are many ways to implement a RESTful structure.


As for HTML5 forms supporting PUT & DELETE, it's really up to the browsers to start supporting different methods rather than the spec itself. If all the browsers started implementing different methods for form submission, I'm sure they'd be added to the spec.

With the web going the way it is, a good RESTful implementation is liable to also incorporate some form of AJAX anyway, so to me it seems largely unnecessary.

like image 135
zzzzBov Avatar answered Sep 22 '22 22:09

zzzzBov