我试图设计一个 RESTful API,用户可以在一个 GET 请求中获取单个产品或产品列表。每个产品都有一个唯一的 ID。
单个产品的 URL 非常简单:
http://mycompany.com/api/v1/product/id
这将返回单个产品的信息。对于多个产品信息的 URL 应该是什么样子,我感到困惑。
怎么样
http://mycompany.com/api/v1/product/ids
其中 id 是逗号分隔的 id 列表?
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:
GET
https://example.com/api/v1/products
And filter the response list by id:
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