分层 RESTful URL 设计
我仔细阅读了有关此问题的问题,但仍然没有明确的答案。
我有一个应用程序,想要构建一个 RESTful API 来公开一部分信息。我有三个资源:
- 用户
- 报告
- 照片
用户有报告,报告有照片。照片不能存在于报表之外,报表也不能存在于用户之外。
我根据我的要求设计了以下 URL
用户登录,服务器使用在所有 API 调用的标头中发送的令牌进行响应
GET example.com/api/
获取用户信息
GET example.com/api/users/{username}
获取所有用户报告
GET example.com/api/users/{username}/reports
获取报告的所有照片
GET example.com/api/users/{username}/reports/{report_id}/photos
添加照片
POST example.com/api/users/{username}/reports/{report_id}/photos
删除照片
DELETE example.com/api/users/{username}/reports/{report_id}/photos/{photo_id}
修改照片描述
PUT example.com/api/users/{username}/reports/{report_id}/photos/{photo_id}
问题
- 在 URL 中添加资源 id 是否是一种好的做法,即资源/id,或者应该将其添加为查询参数?
- 这种链接资源(即资源/id/子资源/id/等)的方法是否可以接受且良好,或者我应该将所有资源放在顶层并使用查询参数指定其位置?
I have perused the questions asked about this, but I still don't have a definitive answer.
I have an application and would like to build a RESTful API to expose a subset of information. I have three resources:
- users
- reports
- photos
Users have reports and reports have photos. Photos cannot exist outside of reports and reports cannot exist outside of users.
I have designed the following URLs for my requirements
User login, server responds with token which is sent in the header of all API calls
GET example.com/api/
Get user info
GET example.com/api/users/{username}
Get all user reports
GET example.com/api/users/{username}/reports
Get all photos of a report
GET example.com/api/users/{username}/reports/{report_id}/photos
Add a photo
POST example.com/api/users/{username}/reports/{report_id}/photos
Delete a photo
DELETE example.com/api/users/{username}/reports/{report_id}/photos/{photo_id}
Modify photo description
PUT example.com/api/users/{username}/reports/{report_id}/photos/{photo_id}
Questions
- Is it good practice to add a resource id in the URL, i.e. resource/id, or should this rather be added as a query parameter?
- Is this method of chaining resources, i.e. resource/id/sub-resource/id/etc., acceptable and good or should I put all my resources at the top level and specify its position with query parameters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个设计没有错。但是这会创建很长的 URL,有时很难理解,并且 API 的用户需要了解层次结构。此外,API 的使用者需要以一点点非标准的方式编写更多代码(即使它可以完成,但会有点混乱)。从不同的角度思考这个问题
您拥有三个资源,每个资源都有自己的身份。因此,如果我们重构上述 URI,它将如下所示(我仅演示 GET)
用户资源:
获取用户列表
获取特定用户
报告资源:
获取所有报告
获取特定报告
照片资源
照片
特定照片
用户 所有报告
用户的特定报告
用户 所有照片
用户 报告 ID 的所有照片(如果 report_id 与用户无关,则您可能不需要用户名,这将进一步简化 URI)
所有 报告
这个简化了理解,并且可以使用这种方法在消费者端编写更标准的代码。
Nothing wrong in this design.But this creates long URL which sometime are difficult to understand and the user of the API needs to know the hierarchy.Moreover the consumer of the API need to write more code in little bit non-standard way(Even though it can be done, but will be little messy). Think this from a different perspective
You have three resources and each has its own identity.So if we refactor the above URI's it will looks like below (I am demonstrating only GET)
User Resource:
Get users list
Get specific user
Report Resource:
Get all reports
Get a specific report
Photo Resources
All Photos
Specific Photo
User All Reports
Specific report of a user
User All Photos
User All Photos for a report id (You may not need user Name if report_id is unique irrespective of the user, which will further simplify the URI)
All photos of a report
This simplifies the understanding and more standard code can be written on the consumer side using this approach.
恕我直言,你建模得很好。
关于
1
我宁愿使用resource/id
而不是查询参数。但建模时必须记住的一件事是代理的缓存机制等等。所以不要忘记标题。我寻找用于过滤和排序的查询参数。
关于登录,凭据应位于标头中,不需要特定资源。只需根据资源安全性应用即可。
IMHO you are modelling it well.
Regarding
1
I'd rather go withresource/id
rather than query param. But one thing you must have in mind when modelling is the cache mechanism by proxy and so on. So do not forget the headers.I go for query params for filtering and those sorts.
About the login, the credentials should be in the headers, and no specific resource is needed. Just apply per resource security.
我不认为你的计划有什么问题。
现在大多数框架都使用类似的标准来指定 url(例如 Django)。
在我个人看来,它使 URL 更具可读性,对用户来说也更友好。
I don't see anything wrong with your scheme.
Most frameworks nowadays use a similar standard for specifying url's (like Django).
In my personal opinion, it makes the URL more readable and a bit nicer for the user.