Dave New
Dave New

Reputation: 40082

REST: GET filtering convention

When following a REST approach, which of the following GET requests is more correct:

v1/companies/10/employees/state=approved

v1/companies/10/employees?state=approved

There seems to be mixed use online.

Upvotes: 0

Views: 454

Answers (2)

Marlon
Marlon

Reputation: 898

Only the second approach makes sense to me. In a RESTful approach, you have to use the QueryString in order to filter the returned resources. In this case, add in your querystring the resource attributes that you want to use as filters. For example:

v1/companies/10/employees?state=approved&attribute2=value&attribute3=value

The first URI doesn't make any sense to me because you're doing a mix between URI and QueryString to filter the result.

v1/companies/10/employees/state=approved

Just to clarify, let's look at another example. The URI below only makes sense if "state" and "approved" are resources. But if they're only attributes from employee, then it's not RESTful. Remember, design your URIs considering that you're exposing resources rather than methods or actions.

v1/companies/10/employees/state/approved

Upvotes: 1

MichaelLo
MichaelLo

Reputation: 1319

I think it depends rather state is a resource or not. REST conventions are Controller/Resource/Sub -Resource, for example: Books/Comics/SuperMan/FirstEditon rather than Books/Comics/SuperMan?edition=FirstEdition

More readable, and indicates that FirstEdition is a resource rather than a state.

Upvotes: 0

Related Questions