RESTful 操作方式

发布于 2024-12-03 06:18:58 字数 594 浏览 0 评论 0原文

我想要一个不违反 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆追雨的时光 2024-12-10 06:18:58

您的资源是一个房间,因此首先您必须考虑:

POST /room

没有。响应将返回 Content-Location : /room/{roomId} 标头,该标头指示您房间 URI。

GET /rooms

列出所有房间。

然后,您可以考虑用于加入操作的资源 URI:

/room/{roomId}/join/{joinId}

对于用户加入特定房间:

POST /room/{roomId}/join --data <join userId="{userId}" />
Response header : **Content-Location : /room/{roomId}/join/{joinId}** 

对于用户离开特定房间:

DELETE /room/{roomId}/join/{joinId}

对于特定房间的“加入”列表:

GET /room/{roomId}/joins
Response content : 
<joins>
 <join id="888" userId="100" />
 <join id="889" userId="101" />
 <join id="890" userId="102" />
</joins>

对于特定用户:

GET /user/{userId} 
Response content : 
<user id="100" name="john" />

对于允许的用户:

POST /room/{roomId}/allowed 
--data 
<allowed>
 <user id="100">
 <user id="101">
 <user id="102">
 <user id="103">
</allowed>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文