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

24,584

Solution 1

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.

Solution 2

I've noted 2 approaches to RESTful URI design: hierarchical & filtered

I feel hierarchical is overly verbose, has the potential for redundant endpoints (not DRY) and disguises in what resource's state you're really interested (after all, REST = representational state transfer).

I favor Simple URIs

Simple is elegant. I'd choose a URI structure like

GET http://server/products/789

because I am interested in the state of the product resource.

If I wanted all products that belonged to a specific shelf at a specific store, then I would do

GET http://server/products?store=123&shelf=456

If I wanted to create a product at a specific store on a specific shelf then I'd post

{
    product: {
        store: 123,
        shelf: 456,
        name: "test product"
    }
}

via

POST http://server/products

Ultimately, it's tomayto, tomahto

REST doesn't require one over the other. However, in my own experience, it is more efficient to consume a RESTful API that maps single entities to single endpoints (eg: RestKit object mappings on iOS) instead of having an entity map to many different endpoints based on what parameters are passed.

About REST

As far as REST, it is not a protocol and has no RFC. It is tightly related to the HTTP/1.1 RFC as a way to implement its CRUD actions, but many software engineers will posit that REST does not depend on HTTP. I disagree and consider such as conjecture, because the original dissertation by UCI's Roy Fielding (http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm) explains the deep rooted connection of REST and HTTP/1.1. You may also enjoy Roy's opinion on the topic: http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven.

The principles defined by REST can be applied to other protocols, but REST was built for the internet and HTTP is the protocol for the world wide web.

REST vs RPC

RPC is all about making calls to remote functions and is verb-centric.

REST is all about using the CRUD convention to act on data depending on how the CRUD operation applies to a given data type and is noun-centric.

You can accomplish the same things with REST or RPC, but REST follows DRY principles because for every URI you can perform 4 actions whereas RPC requires an endpoint for each action.

PS

Much of this is my opinion and based on my experiences, but I hope it sheds some light on how you could most efficiently design a RESTful URI schema. As always, your specific goals and needs will affect your choices, but simplicity is always a good target for which to aim.

Solution 3

Creating a product should just be a POST to

http://server/product

Updating a product should just be a PUT to

http://server/product/$id

Getting a product should just be a GET to

http://server/product/$id

Deleting a product should just be a DELETE to

http://server/product/$id

You should use the http methods that are there for you to get more functionality out of a simpler uri structure. If creating a product requires a passing in a store and shelf as a requirement, then those should get passed in the body of your POST (or PUT if you're changing shelves).

When someone does a GET to http://server/product/$id, they will get back some kind of xml/json response, right? What does that look like? The incoming data for a create or update should be POSTed or PUT the same way in the body of the request. That is how you pass in the store and shelf, not via the uri. The uri should be as simple as possible and just point to the resource (product), using the http verbs to differentiate functionality.

If you want to be able to get the contents of shelf 23, you do a GET to

http://server/shelf/23

When you do, you get back a json / xml / custom media type document that has the shelf data and a collection of product elements with links back to their product uri.

If you want to be able to move product 23 from one shelf to another, you do a PUT to

http://server/product/23 

In the body of the PUT you have the product in the representation of your choice, but with the updated shelf.

It's a weird mode of thinking at first, because you're not dealing with functionality across the entire system, but instead focusing on the resources (product, shelf, store) and using the http verbs to expose them to the universe.

Solution 4

Since products may be in several stores or several shelves (categories?), I'd have each product have a unique number regardless of its position in the hierarchy. Then use the flat product number. That makes the API more stable when some products are for instance moved in your store.

In short, don't add unneeded redundancy to your API. To get a shelve list a store ID is enough, for a product list a shelve ID is enough... etc.

Share:
24,584
Daniel T.
Author by

Daniel T.

Updated on January 11, 2020

Comments

  • Daniel T.
    Daniel T. over 4 years

    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.