In fact, you should use HTTP methods indicate the semantics of the request and use representations to manipulate the state of the resources on the server.
To add a given category to a product, you could perform a POST
request to /products/{productId}/categories
, where the request payload contains a representation of the reference of the category being added. For example:
POST /products/foo/categories HTTP/1.1
Host: example.org
Content-Type: application/json
{
"categoryId": "bar"
}
The response of a successful request could be like:
HTTP/1.1 201 Created
Location: /products/foo/categories/bar
If idempotency is a concern, you can use PUT
to create a resource, as long as the resource identifier is provided by the client. This answer I put together a while ago clarifies this. Then your request would be like:
PUT /products/foo/categories/bar HTTP/1.1
Host: example.org
Content-Type: application/json
And the successful response would be much the same as the one shown above.
To remove a given category from a given product, you could use a DELETE
request to /products/{productId}/categories/{categoryId}
:
DELETE /products/foo/categories/bar HTTP/1.1
Host: example.org
And the response of a successful request could be like:
HTTP/1.1 204 No content