I have a REST API resource that can be fetched using one of multiple identifiers. Sometimes a client might want a specific resource, and sometimes they might want a set of resources based on a certain field.
For an easy example, let's say some clients store a resourceId
and other clients store a resourceNumber
. In addition, a client might want to fetch all resources where resourceCategory
equals a specific value.
There are several approaches I can take to implement this, and I would like to know if there is a best practice or one approach is preferred.
Have a single route such as
GET /api/resource
with parameters forresourceId
,resourceNumber
andresourceCategory
. Based on the parameters provided, return the corresponding resource(s).Have multiple routes such as
GET /api/resource/id/{resourceId}
GET /api/resource/number/{resourceNumber}
GET /api/resource/category/{resourceCategory}
.
Approach #1 allows someone to specify multiple options, which in some cases could be good but in other cases doesn't make sense (i.e. a resourceId
and resourceNumber
could be mutually exclusive)
I have done a lot of googling and researched the OpenAPI spec (which I am following), but have failed to find anything that addresses this issue.
Please Note!!! I am not looking for individual opinions. I want to follow any best practices / standards that exist, so I am asking if there is a best practice or standard such as OpenAPI that covers this scenario for RESTful web services.
I am also curious as to which approach is more commonly used, although I realize this can be difficult to determine.