views:

216

answers:

3

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?

A: 

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.

Kevin
-1: That's no less an RPC call than the original question. He asked for a RESTful way and putting the action in the body isn't.
Greg Beech
Actually I think it is. REST signifies that the object is located by the URL location and can always re-access that item by going to that URL. The POST event shows that the calling object wishes to perform an action on that item.
Kevin
However, RESTful indicates that the HTTP verb (GET, PUT, DELETE) etc. indicates the action to perform. Putting the action in a POST body is RPC-style, not RESTful.
Greg Beech
I see what you are saying. But if you are going to potentially do multiple things with the PUT command (I misspoke when I said post) you have to tell it which action to do.
Kevin
True - but then that isn't RESTful, it's RPC-style. And you're right, it can be necessary to have RPC-style pockets within a predominantly RESTful API as sometimes there's simply no other way you can model what you're trying to do. But in this case the GET/PUT combination shown in one of the answers would allow it to be modeled RESTfully just fine. (PS: I removed the -1 vote because I think the answer plus comments are useful to the question).
Greg Beech
Yeah your right, after I saw the other response, it became clear that was the obvious answer.
Kevin
+5  A: 

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

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

Paul Morgan
indeed solves the problem, but it requires two roundtrips to the server
LiorH
No it wouldn't you can do it in one trip. All you need is the URI for the item you want to copy and place that information in the post of the new object's URI.
Kevin
Is performance such an issue you can't do two roundtrips to the server? KISS
Paul Morgan
+1  A: 

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.