Reputation: 41665
Suppose there's USERS and ORDERS
for a specific user's order list
You could do
/user/3/order_list
/order/?user=3
Which one is prefered and why?
Upvotes: 3
Views: 40
Reputation: 3541
The whole point of REST is resources. You should try and map them as closely as possible to the actual requests you're going to get. I'd definitely not call it order_list
because that looks like an action (you're "listing" the orders, while GET
should be enough to tell you that you're getting something)
So, first of all I think you should have /users
instead of /user
, Then consider it as a tree structure:
So, I'd go for something like:
yourdomain.com/my/users
yourdomain.com/my/users/3
yourdomain.com/my/users/3/orders
yourdomain.com/my/users/3/orders/5
Upvotes: 0
Reputation: 8897
When I started to create an API, I was thinking about the same question.
Video from apigee. help me a lot.
In a nutshell when you decide to build an API, you should decide which entity is independent and which is only related to someone.
For example, if you have a specific endpoint for orders with create/update/delete operations, then it will be fine to use a second approach /order/?user=3
.
In the other way, if orders have only one representation, depends on a user and they don't have any special interaction then you could first approach.
There is also nice article about best practice
Upvotes: 0
Reputation: 1154
Optional parameters tend to be easier to put in the query string.
If you want to return a 404 error when the parameter value does not correspond to an existing resource then I would tend towards a path segment parameter. e.g. /customer/232 where 232 is not a valid customer id.
If however you want to return an empty list then when the parameter is not found then query string parameters. e.g. /contacts?name=dave
If a parameter affects an entire URI structure then use a path e.g. a language parameter /en/document/foo.txt versus /document/foo.txt?language=en
If unique identifiers to be in a path rather than a query parameter.
Path is friendly for search engine/browser history/ Navigation.
Upvotes: 1