HTTP Method to use for adding to a collection in a RESTful API
Asked Answered
A

2

8

I have a resource that represents a collection of tags:

/users/{username}/tags

An API client should be able to add a set of tags to this collection in a single HTTP request. I thought about how to do this and first thought about using the PUT or POST methods. However I think this would imply that the client is "setting" or "replacing" the tags in that collection. What would be the most appropriate HTTP method (or perhaps a different mechanism) to "add" multiple tags to that collection?

{HTTP METHOD} /users/{username}/tags

Request Body:

 ["short", "crazy", "funny"]
Appoint answered 1/6, 2012 at 18:16 Comment(0)
A
11

If the tags being sent in the request body are intended to be added to a collection, rather than replace, I would suggest POST. If you intend to replace the existing tags, use PUT.

Addy answered 1/6, 2012 at 18:22 Comment(0)
C
13

PUT /users/alganet/tags replaces all the tags.

POST /users/alganet/tags adds more tags.

You can also use PATCH.

PATCH /users/alganet/tags changes tags.

Possible body:

{"POST":["rest", "php"], "DELETE":["soap"]}

The body must have a specific patch format matching the Accept-Patch header. The sample body above is a custom format for patches, but you could use a clean diff for example.

Coiffure answered 1/6, 2012 at 20:33 Comment(0)
A
11

If the tags being sent in the request body are intended to be added to a collection, rather than replace, I would suggest POST. If you intend to replace the existing tags, use PUT.

Addy answered 1/6, 2012 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.