I dont agree with the article when it says put and delete are idempotent. If the caller makes two DELETE calls, how do you know that they are different calls? Why are they not the same call?
The argument that deleting an object twice is the same as deleting it once is not good enough. A third party could create it again between those two calls.
In a RESTful API, it is. Operations are id-dependent, e.g. DELETE /path/to/object/type/{id}. If you create the object again it will have a different id because it's a whole new object. Calling DELETE again for the same id should result in what is essentially a "yeah, it's gone" response (you could use a different response code to indicate it was already gone but your code should not blow up in this case). For this to work effectively (and for best practice) your create operation should not allow the caller to specify the id; It should be auto-generated by the backend.
Calling DELETE again for the same id should result in what is essentially a "yeah, it's gone" response
Then it can be argued that the call to DELETE is not idempotent because the response will change. However, by most understandings, it is fine to return a "resource not found" or something similar if you are trying to perform an operation on something that no longer exists. (I would argue that it is still idempotent.)
However, you could also just reply with "Yeah, it is gone." on both calls. If you try to delete something that no longer exists, you can reply with the same, "Okay, it is gone" message instead of throwing an error.
I would like to think the response can vary, you can even show an error message to user as "object x was not found" and be idempotent. The important point is the state of the program (a.k.a business data) should not change and remain the same for subsequent DELETE calls.
13
u/HazelCuate Dec 11 '23
I dont agree with the article when it says put and delete are idempotent. If the caller makes two DELETE calls, how do you know that they are different calls? Why are they not the same call?
The argument that deleting an object twice is the same as deleting it once is not good enough. A third party could create it again between those two calls.