我已经阅读了关于这个问题的提问,但仍然没有一个确定的答案。
我有一个应用程序,想建立一个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}
问题
- 将资源 ID 添加到 URL 中,例如 resource/id,是好的实践吗?还是应该将其作为查询参数添加?
- 这种链接资源的方法,例如 resource/id/sub-resource/id/等是否可接受和好的做法,或者我应该将所有资源放在顶层,并使用查询参数指定其位置?
example.com/api/…
与example.com/api/1/…
)。 - Regexident