How to implement copy paste of a resource in REST?

断了今生、忘了曾经 提交于 2019-12-01 06:27:14

问题


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?


回答1:


Copy = GET http://mydomain.com/book/1

Paste = PUT http://mydomain.com/book/2 or POST http://mydomain.com/book




回答2:


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:


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.



来源:https://stackoverflow.com/questions/859445/how-to-implement-copy-paste-of-a-resource-in-rest

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