REST - revertable DELETE

不打扰是莪最后的温柔 提交于 2019-12-05 03:08:47

You could use the trashcan approach.

DELETE http://company/api/x/

causes x to be moved to the trashcan. At which point it could be accessed with,

GET http://company/api/trashcan/x/

and if you wanted to restore it, then take the retrieved representation and do

PUT http://company/api/x/

UPDATE:

Using hypermedia makes it a bit more obvious to the client what they are supposed to do.

GET http://company/api/trashcan/x
=>
200 OK

<x-resource>
  <description>This is the object I deleted</description>
  <link rel="restoreto" href="http://company/api/x/" />
</x-resource>

Having thought about it some more, PUT is really the right method. It is unsafe, idempotent and you know the URI where to restore the file too. If fits the semantics of PUT perfectly. An alternative to rel="restoreto" might be rel="originallocation".

You have a resource that can exist in any of several states (active, marked_for_deletion, deleted) with actions that move the resource from one state to another. This is the classic definition of a state machine. For a given transition, you find yourself asking "what HTTP verb represents that specific transition? do I have to abuse one? is POST a catch-all? can I invent my own?" There is a better way. Expose the state directly, and use GET and PUT to modify it. Let the server figure out how to get from the former state to the new state. For example, you might have a resource:

GET /foo -> {"a": 1, "b": 2, "status": "active"}

You want to change it so that:

GET /foo -> {"a": 1, "b": 2, "status": "marked"}

You might well wonder whether DELETE is appropriate for that, or some custom method, or you can just PUT that new state and be done:

GET /foo -> {"a": 1, "b": 2, "status": "active"}
PUT /foo <- {"a": 1, "b": 2, "status": "marked"}
GET /foo -> {"a": 1, "b": 2, "status": "marked"}

RESTful means you can use POST to perform a delete if you wanted to since: Unlike SOAP-based web services, there is no "official" standard for RESTful web services.

In this case, I would change your Mark For Delete to a POST (it's actually an update and not a delete).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!