RESTful 操作方式
我想要一个不违反 REST 原则的 RESTful 接口。这是更多讨论的问题,您将如何做以及您认为最好的解决方案是什么。想象一下这个应用场景。
应用程序具有典型的用户和他们聚集进行交互的房间。每个 HTTP 请求都包含 HTTP 基本身份验证标头,其中包含用户与资源交互的信息。让我们考虑一下 /rooms URL 下的房间资源。我希望有 RESTful 方法+URI 来处理用户创建房间(并且不加入他,只提供可以加入该房间的数据)、加入房间和离开房间时的操作。我想到的是:
创建房间
POST /rooms --data {room data}
加入和离开房间可能看起来像下面的代码,
PUT/DELETE /rooms/{roomId}/{userId}
如您所见,我需要传递 userId,它应该是来自 HTTP 标头的上下文信息,所以我猜我不应该在 URL 中传递它。这里的问题是,在创建房间期间,我让用户进入房间,但他们具有“not_joined”状态。因此,在创建之后(到目前为止尚未执行任何连接),实际上存在 /rooms/{roomId}/{userId} 资源。知道如何做得很好吗?:-)
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 rooms 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?:-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的资源是一个房间,因此首先您必须考虑:
没有的。响应将返回 Content-Location : /room/{roomId} 标头,该标头指示您房间 URI。
列出所有房间。
然后,您可以考虑用于加入操作的资源 URI:
对于用户加入特定房间:
对于用户离开特定房间:
对于特定房间的“加入”列表:
对于特定用户:
对于允许的用户:
Your resource is A room so firstly you must consider :
Without the 's'. The response will return the Content-Location : /room/{roomId} header which indicates you the room URI.
Lists all the rooms.
Then you can consider a resource URI for join action :
For a user to join a particular room :
And for a user to leave a particular room :
For a list of 'joins' of a particular room :
For a particular user :
For allowed users :