What's the correct way to view idempotency in terms of HTTP DELETE?

前端 未结 3 1515
轮回少年
轮回少年 2021-02-02 15:41

I have spent a lot of time recently reading the HTTP 1.1 specification and relating it to REST. I have found that there are two interpretations of the HTTP DELETE method in rega

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-02 16:29

    @MichaelBurr is correct about idempotency and side-effects.

    My opinion is that there are 2 states involved in a given REST request, the client's state and the server's state. REST is all about transferring these states between the server and the client, such that the client's state maps to a subset of the server's state, in other words, the subset stays consistent with the server. Because of that idempotency should mean that subsequent idempotent requests will not result in either state being different than it would be from only making the request once. With the first DELETE you would imagine that the server deletes the resource and lets the client know it can delete the resource as well (as the resource "doesn't exist anymore"). Now both states should be identical to before with minus the item that was deleted. For the client to do anything different when it tries to delete the item after it has already been deleted, then the state that is transfered from the server to the client must contain different information. The server can do things slightly differently with the information that the resource was already deleted, but once it responds with something different idempotency of the methods is essentially broken.

    For idempotent function:

    delete(client_state) -> client_state - {item}
    delete(delete(client_state)) -> client_state - {item}
    delete(client_state) = delete(delete(client_state))
    

    The best way to guarantee this idempotency is if the server's response is identical, that means the only way for the client's state to break the idempotency is for there to be non-determinacy or side effects in the client's handling of the response (which probably points to an incorrect implementation of handling the response).

    If there is an agreement between the client and server that the status codes exist outside of the representation of the state being transferred (REST), then it is possible to inform the client that the item "doesn't exists anymore" (as it would in the first request) with the extra comment that it had previously been deleted. What the client does with this information is unclear, but it shouldn't effect the resulting client state. But then the status code can't be used to communicate state, or rather if it does also communicate state in other situations (like maybe "you don't have permission to delete this item" or "item was not deleted"), then there's some introduced ambiguity or confusion. So, you at least need a pretty good reason for introducing more confusion into the communication if you want to say that DELETE is idempotent and still have the server's response depend on previous DELETE requests that are identical.

    HTTP requests involve remove methods, so the function might resemble

    delete(client_state) = send_delete(client_state) -> receive_delete(client_state) 
                                                     -> respond_to_delete(informative_state) 
                                                     -> handle_response(informative_state) 
                                                     -> client_state - {item} 
    

提交回复
热议问题