I\'m new to REST and I\'ve observed that in some RESTful services they use different resource URI for update/get/delete and Create. Such as
The premise of using /resources
is that it is representing "all" resources. If you do a GET /resources
, you will likely return the entire collection. By POSTing to /resources
, you are adding to the collection.
However, the individual resources are available at /resource. If you do a GET /resource
, you will likely error, as this request doesn't make any sense, whereas /resource/123
makes perfect sense.
Using /resource
instead of /resources
is similar to how you would do this if you were working with, say, a file system and a collection of files and /resource
is the "directory" with the individual 123
, 456
files in it.
Neither way is right or wrong, go with what you like best.
My two cents: methods who spend their time changing from plural to singular or viceversa are a waste of CPU cycles. I may be old-school, but in my time like things were called the same. How do I look up methods concerning people? No regular expresion will cover both person and people without undesirable side effects.
English plurals can be very arbitrary and they encumber the code needlessly. Stick to one naming convention. Computer languages were supposed to be about mathematical clarity, not about mimicking natural language.
Have a look at Google's API Design Guide: Resource Names for another take on naming resources.
In short:
|--------------------------+---------------+-------------------+---------------+--------------|
| API Service Name | Collection ID | Resource ID | Collection ID | Resource ID |
|--------------------------+---------------+-------------------+---------------+--------------|
| //mail.googleapis.com | /users | /name@example.com | /settings | /customFrom |
| //storage.googleapis.com | /buckets | /bucket-id | /objects | /object-id |
|--------------------------+---------------+-------------------+---------------+--------------|
It's worthwhile reading if you're thinking about this subject.
To me plurals manipulate the collection, whereas singulars manipulate the item inside that collection.
Collection allows the methods GET / POST / DELETE
Item allows the methods GET / PUT / DELETE
For example
POST on /students will add a new student in the school.
DELETE on /students will remove all the students in the school.
DELETE on /student/123 will remove student 123 from the school.
It might feel like unimportant but some engineers sometimes forget the id. If the route was always plural and performed a DELETE, you might accidentally wipe your data. Whereas missing the id on the singular will return a 404 route not found.
To further expand the example if the API was supposed to expose multiple schools, then something like
DELETE on /school/abc/students will remove all the students in the school abc
.
Choosing the right word sometimes is a challenge on its own, but I like to maintain plurality for the collection. E.g. cart_items
or cart/items
feels right. In contrast deleting cart
, deletes the cart object it self and not the items within the cart ;).
Plural
orders/
gets an index list of orders.For example:
GET /resources
- returns a list of resource items
POST /resources
- creates one or many resource items
PUT /resources
- updates one or many resource items
PATCH /resources
- partially updates one or many resource items
DELETE /resources
- deletes all resource items
And for single resource items:
GET /resources/:id
- returns a specific resource item based on :id
parameter
POST /resources/:id
- creates one resource item with specified id (requires validation)
PUT /resources/:id
- updates a specific resource item
PATCH /resources/:id
- partially updates a specific resource item
DELETE /resources/:id
- deletes a specific resource item
To the advocates of singular, think of it this way: Would you ask a someone for an order
and expect one thing, or a list of things? So why would you expect a service to return a list of things when you type /order
?
From the API consumer's perspective, the endpoints should be predictable so
Ideally...
GET /resources
should return a list of resources. GET /resource
should return a 400 level status code.GET /resources/id/{resourceId}
should return a collection with one resource.GET /resource/id/{resourceId}
should return a resource object.POST /resources
should batch create resources.POST /resource
should create a resource.PUT /resource
should update a resource object.PATCH /resource
should update a resource by posting only the changed attributes.PATCH /resources
should batch update resources posting only the changed attributes.DELETE /resources
should delete all resources; just kidding: 400 status codeDELETE /resource/id/{resourceId}
This approach is the most flexible and feature rich, but also the most time consuming to develop. So, if you're in a hurry (which is always the case with software development) just name your endpoint resource
or the plural form resources
. I prefer the singular form because it gives you the option to introspect and evaluate programmatically since not all plural forms end in 's'.
Having said all that, for whatever reason the most commonly used practice developer's have chosen is to use the plural form. This is ultimately the route I have chosen and if you look at popular apis like github
and twitter
, this is what they do.
Some criteria for deciding could be:
So it's up to you. Just whatever you do be consistent.