Say I have a "user" resource and a "group" resource. RESTful representations of them would look like;
/user/:id
/group/:id
But what if I want to perform an operati开发者_如何转开发on on the association between the two resources? For example, if I wanted to specify that user #1 wanted to join group #1. How would I represent that group#join of group #1 should be executed on user #1?
For example, if I wanted to specify that user #1 wanted to join group #1. How would I represent that group#join of group #1 should be executed on user #1?
I would probably do a POST to something like /groups/1/members or /users/1/memberships. If you want to allow a user to join multiple groups at once, it might make sense to provide an alternative like a PUT to /users/1/memberships.
You have a number of options, but it comes down to the workflow you want to achieve. I think it helps to forget about REST for a little while. Think about building a simple browser app. Where are the links and forms? Each page in your web app is an HTML representation of a resource. It has links and forms. Replace that with alternative representations, but keep the links and forms.
Once you've gone through a design exercise like that, you'll be in a better position to decide whether a POST to /groups/1/members makes sense.
John
Well.. What does /group/:id contain?
Im presuming it will contain a collection of users somewhere?
/group/:id/users/:id - would represent my membership to the group.
Mike Brown
精彩评论