There is /group/1
and /group/2
, and /item/42
is a member of /group/1
. I would like to remove /item/42
from /group/1
and put it into /group/2
. My current solution is this:
GET /group/1/item/42 => Get the item from the first group
POST /group/2/item => Create a clone of the item in the 2nd group
DELETE /group/1/item/42 => Delete the original item from the 1st group
There are (at least) two serious problems with this solution:
- If the client stops before the
DELETE
, the item will become a member of both groups. - The ID of the item won't be the same in
/group/2
, which looks as if the item would lose its identity.
How should I redesign the API if I want to change an item's group membership in a single step (with retaining its identity, if possible)?
In your case, I would not use the URI to link items to groups.
If items to groups are 1 to n-relationship, every item should have a link (e.g. a database foreign key) to its group.
Therefore, your URI-space could be much simpler:
The RESTful way is to modify the item resource with a POST.
In this case, your backend identifies the request as an update to an existing resource.
Here are a few demonstrations how the API could work.
You can use POST instead of PUT if you want.