用户拥有的小部件的 URL 的 RESTful 设计

发布于 2024-10-20 17:00:36 字数 789 浏览 3 评论 0原文

我的 RESTful API 始终具有身份验证,因此所有调用都会针对特定用户进行身份验证。

哪种 URL 的 RESTful 设计比 HTTP 协议更好? 请记住,用户 id 3 已通过基本 http auth/digest 进行了身份验证。

http://server.com/users/3/widgets/ (返回用户的所有小部件编号 3)
http://server.com/users/3/widgets/13 (返回小部件 ID 13)

或:

http://server.com/widgets/(返回用户 ID 3 的所有小部件)< br> http://server.com//widgets/13 (返回小部件 ID 13)

是否更好总是有一个唯一的 URL,例如 http://server.com/users/3/widgets/ 即使知道只有用户 #3 是唯一访问它的人?在每次调用时重新指定 /user/3 是否多余,例如 http://server.com/users/3/widgets/

My RESTful API always has authentication so all calls are authenticated for a particular user.

Which is a better RESTful design of URLs over the HTTP protocol?
Remember that the user id 3 is already authenticated via basic http auth/digest.

http://server.com/users/3/widgets/ (Returns all widgets for user id 3)
http://server.com/users/3/widgets/13 (Returns widget id 13)

or:

http://server.com/widgets/ (Returns all widgets for user id 3)
http://server.com//widgets/13 (Returns widget id 13)

Is it better to always have a unique URL like http://server.com/users/3/widgets/ even know only user #3 will be the only one accessing it? Is it redundant to re-specify /user/3 on every call like http://server.com/users/3/widgets/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

你是暖光i 2024-10-27 17:00:36

我肯定会推荐第一个选项。如果您选择第二个,并且在某个时候您决定允许缓存,那么您必须确保您的变化标头指定表示形式在授权标头上有所不同。如果您使用过期的身份验证令牌,这可能会很痛苦。

这也意味着,如果您想要允许用户查看其他用户的小部件,您可以并且缓存仍然有效。

I would definitely recommend the first option. If you choose the second and at some point you decide you want to allow caching then you would have to make sure that your vary header specified that the representation varies on the authorization header. This can be a pain if you use auth tokens that expire.

It also means that if you ever want to allow users to see the widgets of other users, you can and the caching would still work.

一念一轮回 2024-10-27 17:00:36

从技术上讲,REST 应该是无状态的,因此更“正确”的实现将是您列出的第一种方式。

但是,我的做法与您建议的第一种方法略有不同——用户获得的有关特定小部件的信息是否会根据用户而变化?如果没有,您可能想尝试一下:

http://server.com/users/3/widgets/ (Returns all widgets for user id 3)
http://server.com/widgets/13 (Returns widget id 13)

两全其美。正确的“REST-ful”实现,但是当涉及到特定的小部件时,当前用户并不重要。这样,您的客户也可以更轻松地传递对各个小部件的查询,而无需自己更新查询。如果客户端不应该有权查看该特定小部件,那么使用您已有的身份验证来防范应该不难。

我还基于这样一个假设:小部件列表可能因客户端而异 - 如果情况并非如此,并且所有客户端都将看到相同的小部件列表,则没有理由传递用户,所以采用第二种方式。

REST technically should be stateless, so the more "proper" implementation would be the first way you listed.

However, I would do things slightly differently than how you're suggesting for the first method--will the information a user gets back about a specific widget change, depending on the user? If not, you might want to try this:

http://server.com/users/3/widgets/ (Returns all widgets for user id 3)
http://server.com/widgets/13 (Returns widget id 13)

Gets a best of both worlds. Proper "REST-ful" implementation, but when it comes to specific widgets, the current user doesn't matter. This way, your clients could pass around queries for individual widgets more easily as well--without having to update the query themselves. If the client shouldn't have access to view that particular widget, that shouldn't be difficult to protect against with the authentication you have already.

I'm also basing this all off an assumption that the widget listing could differ from client to client--if that is not true, and all clients are going to see the same widget listing regardless, there's no reason to pass the user, so go with the second way.

魔法唧唧 2024-10-27 17:00:36

我会选择第一个,因为它完全指定了资源。

I'd go with the first because that does fully specify the resource.

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