API 路径应该使用复数名词而不是单数,有什么理由吗?
只是好奇,但我正在制作一个 API,并且到目前为止一直在使用这样的 URL:
/user/create
/user/[id]
现在我正在添加
/user/create/batch
这让我想知道是否有任何好的理由我应该将用户管理端点称为“用户”,而不仅仅是“用户”。话又说回来,也许这根本不重要。我至少猜想我应该在这方面保持一致。
想法?
Just curious, but I am making an API and have been thus far using URLs like this:
/user/create
/user/[id]
and now I am adding
/user/create/batch
This made me wonder if there is any good reason I should call the user management endpoint 'users' rather than just 'user'. Then again, maybe it doesn't matter at all. I would at least guess I should be consistent about this either way.
Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 url 设计中几乎总是使用复数。我总是想到在我的 url 路径中导航,类似于目录,我在其中越来越深入。
有一个顶级资源(如
/users
)表示一个集合,还有一些子资源(如/users/{userId}/items
),它本身就是一个集合。到目前为止我看到的所有 api 及其代表域都符合上述建议。
关于您提到的
/users/create/batch
看起来您想在网址内编码“操作”。在基于 HTTP 的静态设计中,如果您使用 HTTP 方法 POST /users(单用户有效负载)或传输多个用户的批量创建模式,则更适合。在您的情况下,“创建”和“批处理”意味着什么?
I nearly always use plurals in my url design. I always think of navigating in my url paths, similar to directories, where I am descending deeper and deeper.
There is a top resource (like
/users
) which expresses a collection and there are sub-resources (like/users/{userId}/items
) which itself is a collection.All apis and their representing domain I have seen so far would have fit to above recommendation.
Regarding your mentioned
/users/create/batch
looks like you want to encode an 'action' inside url. In a restful design over HTTP it would fit better if you use HTTP methodsPOST /users
(single user payload) or for batch creation mode transmitting multiple users.What does the 'create' and 'batch' mean in your case?
我想说,您应该将“用户”用于与已知特定用户相关的活动,将“用户”用于与所有用户相关的活动(例如全局清理操作),修改用户池(例如您的
create
)或用于获取对已知特定用户的引用。我将使用
/users/select
或/users/search
来查找/获取用户,或者使用/users/cleanup
对所有人执行某些操作用户。我会让
/user/[id]/operation
对特定用户执行某些操作。有一种说法,正如你所说,这根本不重要......
I would say that you should use 'user' for activities relating to a known specific user, and 'users' for activities related to all users (like, say, global cleanup operations), modifying the user pool (like your
create
) or for obtaining that reference to a known specific user.I would have
/users/select
or/users/search
to find/get a user, or/users/cleanup
to do something to all users.I would have
/user/[id]/operation
to do something to/with a specific user.There is an argument that, as you say, it doesn't matter at all...