请求属于集合的对象的首选 REST url 模式是什么?

发布于 2024-12-19 15:51:35 字数 194 浏览 2 评论 0原文

我们有带有一系列通道的 REST API。请求通道时,应该使用通道集合端点还是通道对象端点?

/channels/{id}/     // this?
/channel/{id}/      // or this?

当涉及到主干模型 URL 属性时,我希望它能够与backbone.js 尽可能无缝地工作。

We have REST API with a collection of channels. When requesting a channel, should I use the channels collection endpoint or channel object endpoint?

/channels/{id}/     // this?
/channel/{id}/      // or this?

I want this to work as seamless with backbone.js as possible when it comes to backbone model URL attribute.

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

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

发布评论

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

评论(3

笑饮青盏花 2024-12-26 15:51:35

大多数有关 Restful API 的书籍建议您使用单数作为端点。您可以从那里获取集合以及对集合的单个实例的操作。

打电话:

 GET /channel -> List of channels
 GET /channel/{id} -> Gets a channel
 POST /channel -> Creates a new channel
 PUT /channel/{id} -> Updates an existing channel

等等。然而对于 Backbone 来说这没有什么区别,因为您可以根据需要自定义 URL 行为。

Most books about restful API's suggest that you use the singular for your endpoint. You get the collection from there as well as operations on single instances of the collection.

Calling:

 GET /channel -> List of channels
 GET /channel/{id} -> Gets a channel
 POST /channel -> Creates a new channel
 PUT /channel/{id} -> Updates an existing channel

and so on. Nevertheless for Backbone this makes no difference as you can customize the URL behavior as you wish.

幻梦 2024-12-26 15:51:35

末尾的 / 不应该对偏好产生影响(如果这样做的话,对用户来说太混乱了!),虽然您可以根据需要使用单数或复数,但请在整个应用程序中保持一致让个体成为集合的子元素。将集合视为个人目录。

The / on the end should not make a difference for preference (too confusing for users if it does!) and while you can use singular or plural as you choose, be consistent throughout your app and make the individuals children of the collection. Think of the collection as being like a directory of individuals.

巴黎夜雨 2024-12-26 15:51:35

在某些时候,这确实只是偏好。有些人认为 REST api 的定义应该始终是复数。主要是我总是对 /channel 作为一个列表感到恼火,所以我自然倾向于以复数 /channels 来处理所有内容。这与他们告诉你在表格设计中要做的事情是相反的:将一切都视为单一的。归根结底,只要保持一致就会让许多使用您的 API 的人感到高兴。

At some point its really just preference. Some people argue the definition of REST apis should always be plural. Mainly I always get annoyed with /channel being a list, so I naturally gravitate to treating everything in the plural /channels. Which is backwards from what they tell you to do in table design: treat everything as a singular. At the end of the day, just being consistent will make many people that use your apis happy.

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