danludwig
danludwig

Reputation: 47375

Which URL should be used for GET, PUT, and DELETE of a child resource?

Say I have a resource parent which represents a set of data. A restful URL structure for something like this should be pretty straightforward:

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:

But what about GET, PUT and DELETE? Which of the following are appropriate?

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?

Upvotes: 4

Views: 1687

Answers (3)

user647772
user647772

Reputation:

Is the ID unique only within the parent aggregate? Or is it unique among all children of all parents?

This would depend on the possibility of a child being the cild of one and only one parent. If this is the case, then locally scoped IDs are possible.

If, on the other hand, a child can be child of more than one parent, gloabally scope IDs would be necessary.

Of course one can ask if a child which has a global ID can only exist 'below' a parent, or if such a child can be addressed with an URL that does not contain a parent.

Upvotes: 1

Dmitry S.
Dmitry S.

Reputation: 8503

The GET, PUT or DELETE /api/parents/44/children/6 URL looks to me as it indicates the relationship between the parent and child resources.

The only problem is you would need to know the parent id in order to work with a child resource.

An alterative would be to remove the 'parent' part of the URL all together: GET, PUT or DELETE /api/children/6 and have a "link" attribute or element inside the child resource that provides a URL to its parent.

<child>
   <id>6</id>
   <link rel="parent" href="/api/parents/1" />
   ...
</child>

Upvotes: 0

ioseb
ioseb

Reputation: 16951

@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:

  • GET to "/api/parents/children/6" gives us child resource
  • GET to "/api/parents/children" what happens here? 404?
  • GET to "/api/parents" gives us all parents
  • GET to "/api" hopefully home(or index) document with links to other, subordinated resources.

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.

Upvotes: 3

Related Questions