Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving child resource to another parent in REST

Tags:

rest

I have a REST service.

And now I need to have functionality to move some child resources from one parent to another, for example move a book from one author to another.

My variant is:

POST /api/books/x/moveto/y

But how to create such architect the RESTful way?

like image 288
korolvs Avatar asked Jan 28 '16 09:01

korolvs


1 Answers

From a REST point of view, URLs should be used to locate the resources rather than expressing operations. To express operations, the existing HTTP verbs should be used.

Looks like your "move" operation is all about replacing the author of the book.

And the PUT method seems to be the way to go:

4.3.4. PUT

The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload. [...]

So, you can have an endpoint like the following:

PUT /api/books/{bookId}/author

And the request payload will contain a representation of the new author.

like image 95
cassiomolin Avatar answered Sep 28 '22 09:09

cassiomolin