开发者

Is there any reason why API paths should use plural nouns as apposed to singular?

开发者 https://www.devze.com 2023-03-28 14:04 出处:网络
Just curious, but I am making an API and have been thus far using URLs like this: /user/create /user/[id]

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 开发者_如何学Goshould be consistent about this either way.

Thoughts?


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 methods POST /users (single user payload) or for batch creation mode transmitting multiple users. What does the 'create' and 'batch' mean in your case?


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消