Reputation: 101
I'm creating REST API in which I'm going to assign some items to group:
POST /rest/group/<group1>/item
body
[
{id: 'item1'},
{id: 'item2'},
{id: 'item3'},
...
]
The problem is that I need to cover following use-cases:
I don't have a possibility to use PATCH method.
What is the best option to archive this?
One that I have in mind is something like this:
[
{id: 'item1', action: 'add'},
{id: 'item2', action: 'add'},
{id: 'item3', action: 'remove'},
...
]
I'm asking because maybe there is a more efficient solution for that purpose.
I need to archive ACID also, so don't want to send multiple requests.
Upvotes: 0
Views: 296
Reputation: 173
You don't really need to send actions in your body, as you have HTTP Methods that can express that.
First, to make your request a little more RESTful, I strongly suggest you to use /rest/groups/<group1>/items
, because the guidelines recommend to take resources in plural.
Assigning items to a new group:
(POST) /rest/groups/<group1>/items
With the items inside the json body.
(PUT) /rest/groups/<group1>/items
With the new items to be added inside the json body. In your application, make sure that you recognize it's a (PUT) request, and if so, only add them to the existing list of items. The (POST) should override the entire list so you have a way to quickly change the item collection if needed.
(DELETE) /rest/groups/<group1>/items
Delete the entire item collection for that group.
(DELETE) /rest/groups/<group1>/items/<item1>
Sending an entity body in a (DELETE) method is not forbidden, but it's a practice that most of the known API's tend to avoid. You should be deleting specific items by sending its ID as a path variable.
To batch delete items, you could use query parameters like that:
(DELETE) /rest/groups/<group1>/items?ids=1,5,7
That should delete the items with ID's 1, 5 and 7. Other ways of filtering the deletion can be applied as well, depending on your item model.
Upvotes: 1