apiresturlquery-stringurl-design

What is the correct query parameter for "match-all" in a REST filter when there is a default?


If a REST API endpoint gets all, then filters are easy. Lack of a filter means "get all results".

GET /persons - gets all persons
GET /persons?name=john - gets all persons with name of "john"

But if there is a default, then I need some way to explicitly not set the default filter. Continuing the above example, if each person has a state, "approved" or "pending", and if my system is set such that if I do not explicitly specify a state, it will return all "approved":

GET /persons - gets all approved persons, because defaults to state=approved
GET /persons?state=approved - same thing, gets all approved persons
GET /persons?state=pending - gets all pending persons

How do I get all persons? What if there are 10 possible states? Or 100?

I can think of a few ways:

How do I say in my GET, "override the default for the state to be anything"?


Solution

  • I don't think there is one answer to this question. As long as you document that the default state is approved well I don't think it matter to the clients if you pass any, * etc. All of your proposals are fine except the last one. I don't think that is a good one.

    If I was designing the API I would use all and keep this as a standard. I would also recommend to use paging for all endpoints that returns list of elements. I use offset and limit as paging query parameters. In my API I return 20 elements as default if the client haven't specified another paging criteria.