RESTful delete strategy

后端 未结 5 404
走了就别回头了
走了就别回头了 2021-01-11 14:12

Let\'s say I have a resource that can have two different behaviors when delete is called

  1. The resource is deleted.
  2. The resource is moved to the recycl
相关标签:
5条回答
  • 2021-01-11 14:49

    DELETE should delete the item, no questions asked.

    Sadly, there is no 'MOVE' request in HTTP. POST is usually intended for creating content, PUT is more modifications.

    So I would suggest you you do something like PUT /myresource with some form of meta-data or a json string along the lines of { "recycle":"true" } to indicate that you want to 'recycle' it.

    0 讨论(0)
  • 2021-01-11 14:51

    Why not? You are already passing a parameter to identify which resource, so send another one to establish a different course of action. IMO, it is perfectly RESTful.

    0 讨论(0)
  • 2021-01-11 15:04

    You could also implement 2. as a POST request instead of DELETE.

    POST /myresource
    
    recycle-bin=true...
    

    As in all you're doing is updating the resource to indicate that it is in the recycle-bin.

    EDIT: changed method from PUT to POST given a PUT must enclose a complete replacement (or addition) of the resource, whereas clearly here we are only updating a part of the resource.

    0 讨论(0)
  • 2021-01-11 15:06

    A pure REST strategy should prefer non changing resources. In my opinion, you are not changing the resource by appending a parameter, so it sounds like good strategy to me.

    If you were to perform the same action like so:

    DELETE /myresource.force
    

    that would act like another resource, which wouldn't be optimal.

    0 讨论(0)
  • 2021-01-11 15:06

    Your idea is fine, but I think a custom request header would be a little more appropriate. Query parameters are better suited to, well, parameters.

    A custom request header would look something like this:

    DELETE /myresource
    X-Really-Delete: Yup
    
    0 讨论(0)
提交回复
热议问题