Is it reasonable for REST resources to be singular and plural?

Posted by Evan on Programmers See other posts from Programmers or by Evan
Published on 2012-10-24T14:48:18Z Indexed on 2012/10/24 17:14 UTC
Read the original article Hit count: 227

Filed under:

I have been wondering if, rather than a more traditional layout like this:

api/Products
GET // gets product(s) by id
PUT // updates product(s) by id
DELETE // deletes (product(s) by id
POST // creates product(s)

Would it be more useful to have a singular and a plural, for example:

api/Product
GET // gets a product by id
PUT // updates a product by id
DELETE // deletes a product by id
POST // creates a product

api/Products
GET // gets a collection of products by id
PUT // updates a collection of products by id
DELETE // deletes a collection of products (not the products themselves)
POST // creates a collection of products based on filter parameters passed

So, to create a collection of products you might do:

POST api/Products {data: filters} // returns api/Products/<id>

And then, to reference it, you might do:

GET api/Products/<id> // returns array of products

In my opinion, the main advantage of doing things this way is that it allows for easy caching of collections of products. One might, for example, put a lifetime of an hour on collections of products, thus drastically reducing the calls on a server. Of course, I currently only see the good side of doing things this way, what's the downside?

© Programmers or respective owner

Related posts about rest