Daniel Watkins
Daniel Watkins

Reputation: 1686

RESTfully adding multiple resources as sub-resources

We have two (relevant) types in our system, items and collections. These are identified by ITEM-n and COLL-n (where the ns are integers).

When we want to add a single item to a collection, we do the following:

PUT /collection/COLL-1234/items/ITEM-1234

However, we will sometimes be adding a large number of items to a collection, and would like to avoid many round-trips. One option would be:

PUT /collection/COLL-1234/items

with a payload listing all of the items to add, but we don't want to replace all the items in the collection, so that doesn't quite work.

The best we've come up with is:

PATCH /collection/COLL-1234/items

with a payload listing all of the items. Can anyone suggest anything better?

Upvotes: 0

Views: 167

Answers (1)

user1907906
user1907906

Reputation:

Use POST on the collection resource /collection/COLL-1234/items.

You are right that PUT is meant to replace the collection. But POST creates a new sub-resource. If the representation you POST is not one sub-resource but a list of sub-resource, this is OK, too.

Upvotes: 1

Related Questions