How do I represent self-joined data in a RESTful API

42 views Asked by At

I have products and categories represented by a REST API.
Products are children of categories i.e. /categories/{category_id}/products/{product_id}

Each category can also be child of another category. E.g: Transport -> Cars -> Sports cars -> Aston Martins.

How would I go about representing this RESTfully?

So far I have only come up with: /categories/{category_id}/categories/{category_id}/categories/{category_id}/...
but I don't like how big the URI becomes assuming I can have many levels of subcategory.

1

There are 1 answers

0
arjabbar On

You shouldn't have to use a really long URL to uniquely identify a vehicle. Can a truck and a car have the same id? Can a Toyota and a Ford car have the same id?

Transport -> Cars -> Sports cars -> Aston Martins

could be as easy as:

transports/{id-of-a-particular-Aston-Martin}

or:

transports/{id-of-a-particular-truck}

or if you're viewing a filtered down collection based on some criteria:

transports/?body_style={id for sports car}&make={id for Aston Martin}

The fact that this vehicle is an Aston Martin, a car and a Sport Car doesn't have to be reflected in the URL.