RESTful undelete

后端 未结 6 528
失恋的感觉
失恋的感觉 2021-01-30 12:54

It is a fairly common requirement to support undeletes or delayed/batched deletions for data services. What I\'m wondering is how to implement this in a RESTful way. I\'m torn b

6条回答
  •  北海茫月
    2021-01-30 13:09

    Going by the book: RFC 2616-9.7:

      The DELETE method requests that the origin server delete the resource 
      identified by the Request-URI. This method MAY be overridden by human 
      intervention (or other means) on the origin server. The client cannot
      be guaranteed that the operation has been carried out, even if the 
      status code returned from the origin server indicates that the action
      has  been completed successfully. However, the server SHOULD NOT 
      indicate success unless, at the time the response is given, if it intends
      to delete the resource or move it to an inaccessible location.
    

    When you DELETE a resource the server should mark the resource for deletion on it's side. It doesn't really have to delete the resource, it just can't give any guarantee that the operation has been carried out. Even so, the server shouldn't say it's been deleted when it hasn't.

      A successful response SHOULD be 200 (OK) if the response includes an entity
      describing the status, 202 (Accepted) if the action has not yet been enacted,
      or 204 (No Content) if the action has been enacted but the response does not
      include an entity.
    

    If the operation is delayed, send a 202 and an entity body describing the result of the action. (Think of a poll-able "task" representing the server's deferred deletion of the resource; It could theoretically leave it forever in that state.) All it has to do is prevent the client from retrieving it again in it's original form. Use a 410 for the response code, and when the "task" finishes or the server otherwise deletes the resource, return a 404.

    However, if a DELETE's semantics don't make sense for the resource in question, perhaps it's not a deletion you're looking for, but an addition state transition that alters the resource state but keeps it accessible? In that case, use a PUT/PATCH to update the resource and be done.

提交回复
热议问题