How to implement a copy of a copy of a resource in REST?

How to implement copy-paste support with RESTful?

Say I have a bookstore 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 customer to be able to call a copy of the copy of the book to another store.

Implementation of the following:

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

It seems very RPC. Do you have any suggestion on how this operation can be modeled using RESTful?

+3
source share
3 answers

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

I prefer non-hierarchical resources. The "path" to the file will be just a file property. There are two options for copy-paste.

  • If you really need another path link, add another entry for the path property. The exact same file is in the β€œboth” folders.

  • If you need a new version of the file, and in the future, after changing the changes, create a new resource (another URI) with a different property "path".

  • To move, just change the path property.

If you must insist on hierarchical, just imitate the way the file system copies and pastes.

The copy is simple. GET to copy a resource.

Insert POST because you are creating a new resource, a new URI.

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

If you want, you can specify the location in the deletion request so that the server redirects users who were looking for the moved resource in their previous location.

+1
source

I would have the user execute the PUT command to complete the action.

So, something like a variable in the form data contains the correct action to execute.

0
source

Source: https://habr.com/ru/post/1310157/


All Articles