Reputation: 19815
Example 1:
http://www.example.com/image/logo.png
http://www.example.com/images/logo.png
Example 2:
http://www.example.com/user/johndoe
http://www.example.com/users/johndoe
Especially if you use the Url as REST API. (Example 2)
Which one is recommended and why?
Upvotes: 7
Views: 5000
Reputation: 1313
My personal preference would be to use different url for single and multiple resources.
http://www.example.com/users say that I want all the uses. You can append query parameters to filter the results though.
On contrarily http://www.example.com/user/johndoe - explicitly states that I want a single user whose id is johndoe.
Advantages: 1. Expectation cant be set correctly. 2. /users with no results can be return 201 instead of 404 3. /user/ can return 401 which is error condition.
Upvotes: 1
Reputation: 2571
For REST, i mainly use the plural form to indicate a path to the resource. But you also have to take the Cacheablility, Frequency of Change and the Mutability of the Resource. In my case, collections of the resource is mainly the case, so i had used the plural form.
The reason for this is that for example:
http://www.example.com/users/johndoe
will serve the URI to GET the user johndoe which belongs to your collection of users.
http://www.example.com/users
will be used as the URI to GET all users and can be easily be used on query url like:
http://www.example.com/users?limit=5
creating a new user will still use the same URL then using POST & passing the parameters:
http://www.example.com/users
for refs you may want to check the Oreilly book RESTful Web Services Cookbook
Upvotes: 4