REST api: requesting multiple resources in a single get

Rest

Rest Problem Overview


I'm trying to design a RESTful API where the users can fetch a single product or list of products in a single GET request. Each product has a unique id.

The single product URL is simple enough:

http://mycompany.com/api/v1/product/id

This returns the information for a single product. I'm confused as to how the URL for multiple product information should look like.

How about

http://mycompany.com/api/v1/product/ids

where ids is a comma separated list of ids?

Rest Solutions


Solution 1 - Rest

I would recommend thinking of it like you are listing multiple representations of the resource filtered by id. As such you make a GET request to the base resource:

https://example.com/api/v1/products

And filter the response list by id:

https://example.com/api/v1/products?id=1,2,3

Solution 2 - Rest

Your suggestion of ids separated with commas is good enough.

It would be instructive to examine some public REST APIs to see how they handle. For ex, the StackExchange API separates ids with a semi-colon - https://api.stackexchange.com/docs/answers-by-ids

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser824212View Question on Stackoverflow
Solution 1 - RestabrahamView Answer on Stackoverflow
Solution 2 - RestaldrinView Answer on Stackoverflow