开发者

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 her开发者_StackOverflowe? 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


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜