I want to have RESTful interface which does not break REST principles. This is more discussion question how would you do it and what do you see as the best solution. Imagine this application scenario.
Application has typical users and room开发者_高级运维s where they gather to interact. Each HTTP request includes HTTP basic authentication headers which brings information which user interacts with resource. Lets think of resource of rooms under /rooms URL. I want to have RESTful method+URI to take care of actions when user creates room (and does NOT joins him, only provides data who can join this room), joins room and lefts rooms. What came to my mind is:
Create room
POST /rooms --data {room data}
Join and leave room migh look like code below,
PUT/DELETE /rooms/{roomId}/{userId}
As you can see, I need to pass userId which should be context information from HTTP headers, so I should not pass it in URL I guess. The problem that is here, that during creation of room I got users in room, but they have "not_joined" state. So after creation (no join performed so far), there actually IS /rooms/{roomId}/{userId} resource. Any idea how to do it nicely?:-)
Your resource is A room so firstly you must consider :
POST /room
Without the 's'. The response will return the Content-Location : /room/{roomId} header which indicates you the room URI.
GET /rooms
Lists all the rooms.
Then you can consider a resource URI for join action :
/room/{roomId}/join/{joinId}
For a user to join a particular room :
POST /room/{roomId}/join --data <join userId="{userId}" />
Response header : **Content-Location : /room/{roomId}/join/{joinId}**
And for a user to leave a particular room :
DELETE /room/{roomId}/join/{joinId}
For a list of 'joins' of a particular room :
GET /room/{roomId}/joins
Response content :
<joins>
<join id="888" userId="100" />
<join id="889" userId="101" />
<join id="890" userId="102" />
</joins>
For a particular user :
GET /user/{userId}
Response content :
<user id="100" name="john" />
For allowed users :
POST /room/{roomId}/allowed
--data
<allowed>
<user id="100">
<user id="101">
<user id="102">
<user id="103">
</allowed>
精彩评论