Say I have a resource parent which represents a set of data. A restful URL structure for something like this should be pretty straightforward:
- GET /api/parents – return collection of all parents
- GET /api/parents/1 – return parent with id = 1
- POST /api/parents – add a new parent to the collection
- PUT /api/parents/1 – update data in parent with id = 1
- DELETE /api/parents/1 – delete parent with id = 1
Now say that each parent is composed of a collection of child resources. Should the child resources id’s be considered to have global or local scope? I believe the following 2 URL’s are given:
- GET /api/parents/1/children – return collection of parent 1’s children
- POST /api/parents/1/children – add new child to parent 1’s collection
But what about GET, PUT and DELETE? Which of the following are appropriate?
- GET, PUT or DELETE /api/parents/44/children/6
- GET, PUT or DELETE /api/parents/children/6
It seems like it comes down to the uniqueness scope of the child resource’s ID. Is the ID unique only within the parent aggregate? Or is it unique among all children of all parents? Is one more correct than the other, or does it depend on the id uniqueness scope of the resources in question?
If example 1 is more appropriate than example 2, and parent with id = 44 does not have a child with id = 6 (say child id = 6 belongs to parent with id = 9), what HTTP response should be returned?
@danludwig
i do not think it actually matters as far as URIs are just resource identifiers and all these user friendly hierarchies are important only for human beings… but in either case i’d ask question regarding this one “/api/parents/children/6”, lets break it down:
I think it’s a good test when constructing hierarchical URIs. Just make sure that each segment is bound to particular resource and doesn’t return 404.