Bear Bibeault wrote:It's not really about "changing state". The client has no inkling of state or what's going on behind the scenes on the server.
Rather, it's about the result. As in: is the result the same each time we make the request (excluding outside influences such as someone else changing resources).
Tim Bee wrote:So by result, do you mean response, as in request and response?
What does the transport system care whether it is deleting, adding, updating or creating?
Bear Bibeault wrote:
Tim Bee wrote:So by result, do you mean response, as in request and response?
Yes. Think about your PUT example. If you perform the same PUT over and over, you always get the same response back. Ergo, idempotent.
What does the transport system care whether it is deleting, adding, updating or creating?
It doesn't. What does that have to do with anything?
Paul Clapham wrote:It seems to me that you're envisioning a rule which says "If my transaction is idempotent then I should use Method X, otherwise I should use Method Y". But in fact the rule runs the other way around -- some methods are required to be idempotent and some aren't.
Paul Clapham wrote:It seems to me that you're envisioning a rule which says "If my transaction is idempotent then I should use Method X, otherwise I should use Method Y". But in fact the rule runs the other way around -- some methods are required to be idempotent and some aren't.
Stephan van Hulst wrote:
It's similar for PUT and POST. Using PUT multiple times should always end with the same result. POST indicates that you shouldn't call it repeatedly just willy-nilly.
Tim Bee wrote:I think I understand the general concept of idempotency but I just want to get some further understanding about how this applies to Restful web services.
Tim Bee wrote:But some POST services can be indempotent. You have to know what the service does. It is not that clear cut, as I understand it.
Tim Bee wrote:So if you are doing some kind of testing and want to ensure that the database has the correct starting information, use POST to ensure it is reset, if it has been modified elsewhere.
Ron McLeod wrote:
Tim Bee wrote:But some POST services can be indempotent. You have to know what the service does. It is not that clear cut, as I understand it.
If a POST operation is idempotent, then it is being misused. Do you have an example?
Ron McLeod wrote:
Tim Bee wrote:So if you are doing some kind of testing and want to ensure that the database has the correct starting information, use POST to ensure it is reset, if it has been modified elsewhere.
If you want to set (update) all of the fields (attributes) of a resources to particular vales, then perform a PUT operation and provide complete representation of the resource. If you only want to set specific fields, then perform a PATCH operation, and only provide a representation including the specific fields.
Tim Bee wrote:
Ron McLeod wrote:
Tim Bee wrote:But some POST services can be indempotent. You have to know what the service does. It is not that clear cut, as I understand it.
If a POST operation is idempotent, then it is being misused. Do you have an example?
This is not an example but is the definition from Wiki:
https://en.wikipedia.org/wiki/Idempotence
"...Of the major HTTP verbs, GET, PUT, and DELETE should be implemented in an idempotent manner (according to the standard), but POST need not be..."
Note that it says "need not be", not "cannot be".
Tim Bee wrote:What about someone else changing an attribute somehow else after you submit the PUT?
Tim Bee wrote:I don't agree. What about someone else changing an attribute somehow else after you submit the PUT? Caching will/may prevent it from executing because it is idempotent and has already been executed once. But you want to ensure that the values you originally set are still there.
Stephan van Hulst wrote:
Tim Bee wrote:I don't agree. What about someone else changing an attribute somehow else after you submit the PUT? Caching will/may prevent it from executing because it is idempotent and has already been executed once. But you want to ensure that the values you originally set are still there.
Can't the same be said for GET? If someone else changes a resource after you've cached the response when GETting it, your cached response will be out-of-date. This is a cache-invalidation issue and is not related to whether using POST or PUT is appropriate.
Tim Bee wrote:Let's say I have a RESTful web service that takes some input and I do some basic calculation, like add, multiply, divide.
What method is used for that? You aren't PUTing or POSTing. You aren't really GETing or DELETEing.
Ron McLeod wrote:
Tim Bee wrote:Let's say I have a RESTful web service that takes some input and I do some basic calculation, like add, multiply, divide.
What method is used for that? You aren't PUTing or POSTing. You aren't really GETing or DELETEing.
When the path of the URI contains a verb (such as add), it sounds like a a remote procedure call (RPC) rather than a RESTful operation. RESTful operations are about transfering representations of resource state, not invoking remote procedures.
Tim Bee wrote:Which method do you use and why?
Stephan van Hulst wrote:
POST should create a new resource. POST in general can be used for actions that are not idempotent. Again, a programmer can ignore this, but it would be really weird to use a POST request to get or to replace an existing resource.
If an existing resource needs to be updated (that is, update some fields without replacing the entire resource) you should use PATCH. Since you can send instructions such as "increment the value of this field by 4" in a PATCH request, it's not idempotent in general.
It's fine to make POST and PATCH idempotent, in fact, it will make your application more robust. In general it just doesn't make sense to make POST idempotent, because then you could be using a PUT instead.
Tim Bee wrote:I'm sure I read somewhere that you are supposed to use POST to change just some properties and leave the rest as they were.
Tim Bee wrote:So what you are saying is it is more a case of convention, rather than technical restrictions.
Stephan van Hulst wrote:
Tim Bee wrote:So what you are saying is it is more a case of convention, rather than technical restrictions.
Only if you consider the HTTP specification convention. It's a standard, and lots of things can go wrong if you don't follow standards.
For instance, most browsers give you a warning if you go back in your history, and ask you if your want to resend POST data. I imagine that some browsers will not do this for PUT requests, because PUTs supposed to be idempotent. This can already cause issues if you don't follow standards.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |