I am working on the backend of a web application that exposes a REST-like API to the frontend of the application.
Currently, I am exposing a "Users" resource, where each user can be represented in JSON notation as:
{
"id": "asdfzxcv",
"name": "Morris",
"preferences": {
"dark_mode": true,
"tags": [ "tagA", "tagB" ]
}
}
I currently expose this user at the endpoint GET /api/v1/users/<user-id>.
Now, I have been given the requirement that the frontend should be able to:
- Submit a list of tags to be added to the user's preferences
- Submit a list of tags to be removed from the user's preferences
- Submit a list of tags to replace the user's preferred tags
Originally, I was thinking the client could do PUT (or DELETE) /api/v1/users/<user-id>/preferences/tags/tagA, but this would entail making multiple requests to add/remove multiple new preferences, which would be very wasteful.
How should I design the URL paths to handle this case?
One possible solution is to use query parameters to change the behavior:
POST /api/v1/users/<user-id>/preferences/tags(default action: update/merge)POST /api/v1/users/<user-id>/preferences/tags?action=replacePOST /api/v1/users/<user-id>/preferences/tags?action=delete
Or I could have 3 separate URLs:
POST /api/v1/users/<user-id>/preferences/tags(default action: update/merge)POST /api/v1/users/<user-id>/preferences/tags/replacePOST /api/v1/users/<user-id>/preferences/tags/delete