Reputation: 1622
I've seen questions like this one that were asking about how to handle constructing API's that take multiple ids for a resource and return a list. But my question is if one or more of these ids don't actually match an instance of that resource, what should the response look like?
For example,
data:
{id: 1, value: "one"}
{id: 2, value: "two"}
{id: 3, value: "three"}
request:
.../data/id=1,5,2,6
OR
.../data?ids[]=1&ids[]=5&ids[]=2&ids[]=6
Now, should the endpoint return
(1)
[
{id: 1, value: "one"},
null,
{id: 2, value: "two"},
null
]
OR
(2)
[
{id: 1, value: "one"},
{id: 2, value: "two"}
]
In REST standards, is it important that the order of the resources requested be preserved and/or the size of the list, or should it just return the resources that were found?
I am leaning towards (2) because if you are using the order of list of ids you sent with the request as a direct mapping to the object in the response (3), that seems kinda flaky because it can be an unordered list (and not that useful, because the id should already be in each object).
(3)
1,5,2,6
becomes
[
id=1 => {id: 1, value: "one"},
id=5 => null,
id=2 => {id: 2, value: "two"},
id=6 => null
]
Upvotes: 1
Views: 284
Reputation: 357
On failure: You can return list of failed Id's alongwith suitable HttpStatusCode for failure.
On Success: You can return expected data and 200Ok HttpStatusCode.
So that api-consumer can easily detect success and failure.
Example: I have list of 7 students:[1,2,3,4,5,x,y] and I don't know how many of them are passed exam. Then I will send these Ids to api to check there status. Then
Suppose x and y are invalid Id's, then my api will return [x,y] and statuscode: 400BadRequest.
And If x,y is absent my api will return statuscode:200Ok
Upvotes: 1