How should a REST URL schema look like for a tree hierarchy?

hierarchy, rest, schema, url

Solution

Don't design a REST api based on an URL structure. Here is how I think you should go about designing a REST api.

Trying to define a REST interface without discussing what links will be contained in what resources is like discussing an RPC interface and ignoring parameters and return values.

Problem

Let's assume that I have stores, shelves in a store, and products on a shelf. So in order to get a list of products on a shelf in a store, I'd use the following request: ``` GET http://server/stores/123/shelves/456/products ``` From here, how would I get an individual product? Should I use: ``` GET http://server/products/789 ``` Or: ``` GET http://server/stores/123/shelves/456/products/789 ``` The first method is more concise, since once you get a list of products, you don't really care which store it belongs to if you just want to view the details for a particular product. However, the second method is more logical, since you're viewing the products for a specific shelf in a specific store. Likewise, what about a PUT/DELETE operation? ``` DELETE http://server/stores/123/shelves/456/products/789 ``` Or: ``` DELETE http://server/products/789 ``` What would be the correct way of designing a schema for a tree hierarchy like this? P.S. If I'm misunderstanding something about the REST architecture, please provide examples on how I can make this better. There's way too many people who love to say "REST is not CRUD" and "REST is not RPC", then provide absolutely no clarifications or examples of good RESTful design.

Original source

Related problems