Chris Salij
Chris Salij

Reputation: 3126

Correct RESTful call for this resource

I'm currently designing a RESTful interface to an existing infrastructure and I'm struggling with one particular aspect.

We have a collection of users which can be accessed at /users And elements of that collection can be accessed at /users/1 etc

However each user has a list of friends which needs to be able to accessed as well. Would users/1/friends be the restful call to make here? It doesn't qualify to be a collection of it's own since it's relates to only one user.

Any help would be greatly appreciated

Upvotes: 2

Views: 205

Answers (3)

Darrel Miller
Darrel Miller

Reputation: 142024

As far as RESTful design is concerned, the URI can be whatever you want. However, to ensure your interface can be accessed in a RESTfully you do need to do something like this:

<user>
    <name>Bob Smith</name>
    <link rel="friends" href="/peeps/1/amigos" />
</user>

By adding the extra layer of indirection, your client can discover the link for the list of friends and it no longer matters to the client what that URI is. You can change it whenever you like.

URIs are an implementation detail of the server and clients should never couple on them. The only coupling should be on rels and media types.

Upvotes: 3

fmucar
fmucar

Reputation: 14548

I dont see see anything wrong with your solution. Perfectly good selection as a URI for what it represents.

Upvotes: 1

b_erb
b_erb

Reputation: 21241

I'd say /user/{id}/friends is an appropriate identifier. Of course you can also use /users/{id}/friends, but I personally prefer to differentiate between item resources and collection resources.

Upvotes: 2

Related Questions