How to implement copy paste of a resource in REST?

2020-03-26 07:39发布

How would you implement copy-paste support in a RESTful way?

Let's say I have book store resource. And books in every store

http://mydomain.com/rest/book-stores/1
http://mydomain.com/rest/book-stores/1/books/12

I need the client to be able to invoke copy paste of a book to another store.

Implementing the following:

PUT http://mydomain.com/rest/books/1/copy-paste

seems very RPC like. Do you have any suggestion how can this operation be modeled in a RESTful way?

3条回答
冷血范
2楼-- · 2020-03-26 07:56

This is only a problem if your resources are organized to mimic a hierarchical system. Like a file system.

I prefer non-hierarchical resources. The "path" to a file would just be a property of the file. To copy-paste, there are two options.

  1. If you really just want another "path" reference, add another entry for the "path" property. The same exact file is "in" both "folders".

  2. If you need to new version of the file, effectively forking changes thereafter, create a new resource (different URI) with a different "path" property.

  3. To move, just change the "path" property.

If you must insist on hierarchical, just mimic how a file system does copy-paste and move.

The copy is easy. A GET for the resource to copy.

To paste, a POST, because you are creating a new resource, a new URI.

If you need to do a move, you probably need to DELETE the old resource.

If you want, you can specify a location in the delete request, allowing the server to redirect users looking for the moved resource at its old location.

查看更多
仙女界的扛把子
3楼-- · 2020-03-26 08:17

I would have it so that the user executes PUT command to execute the action.

So something like a variable in the form data is contains the correct action to perform.

查看更多
登录 后发表回答