3

I am designing a RESTful service. It is to list a set of data. The main problem is that the set does not have a reasonable, single identifier. Nor can the specific set be easily calculated within the knowledge of the system. As a result, it does not seem possible to have a GET /items/{identifier} service.

I do have the id of each element being requested. My main issue is that it does not seem RESTful to list the ids in the URI (eg GET items/{id1},{id2},...,{idn} ). Right?

I could see DELETE having a similar use case - remove multiple items in one request cycle.

How would such a use case be satisfied while staying within the REST realm? Is that possible?

1
  • 1
    What RESTful constraint are you concerned about? URIs are simply identifiers that follow the rules laid out in RFC 3986. Personally I see no problem in using a comma separated list of values as part of the URI identifier. Commented Jul 3, 2012 at 3:09

1 Answer 1

0

The approach stated in the question actually means that for each combination of ids there is a resource. Let's say we have 2 ids: 1 and 2.

/items/1,2 /items/2,1 

The above represent different resources, although the result is the same. This might be confusing for the consumer of the API.

Another way to model this is via query parameter as filtering semantics. Let's assume, that the id is actually a field of a resource.

Example, getting item by id 1:

GET /items/1 Response: { "id": 1, "type": "table", "color": "black", ... } 

So the question is, what if I need to get several items as a bulk? You can generalize this question to the general question of filtering items by values on certain fields. E.g.: getting all the items of type table

GET /items?query="name='table'" Response: { "data": [ { "id": 1, "type": "table", "color": "black", ... }, { "id": 2, "type": "table", "color": "grey", ... }, { "id": 6, "type": "table", "color": "brown", ... } ] } 

So the same question can be asked for getting items where id is 1 or 2. Let's say we model the or operation as || in the syntax of query

GET /items?query="id=1||id=2" Response: { "data": [ { "id": 1, "type": "table", "color": "black", ... }, { "id": 2, "type": "table", "color": "grey", ... } ] } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.