REST api: requesting multiple resources in a single get [duplicate]
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?
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
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