【发布时间】:2021-11-14 19:43:52
【问题描述】:
所以我正在做一个小项目。这是一个 REST API,将充当“买卖”网站的后端。 目前我有两个主要资源:用户和广告。 一个用户可以创建许多广告。每个广告都是由某个用户制作的。
有人可以验证以下端点是否遵循 REST 原则并且它们有意义?如果您认为它们看起来不正确,请提出替代方案。
//Users
Create a user - POST /api/users -
user details are passed as json in request body.
Get a user by id - GET /api/users/{user_id}
Get the logged in user - GET /api/users/authenticated_user -
An authentication token is passed in the request header and is used to find the user in the database.
Update the logged in user - PUT /api/users/authenticated_user -
new user details are passed in the request body. An authentication token is passed in the request header and is used to find the user in the database.
Delete the logged in user DELETE /api/users/authenticated_user -
An authentication token is passed in the request header and is used to find the user in the database.
Get an ads user - GET /api/ads/{ad_id}/user
//Ads
Get all ads - GET /api/ads
Create an ad - POST api/ads -
ad details are passed in request body and the user_id of the ad creater is got from the authentication token passed in request header. Would this endpoint make more sense to be something like: /api/users/authenticated_user/ads
Get an ad by id - GET /api/ads/{ad_id}
Update an ad - PUT api/ads/{ad_id} -
ad details are passed in request body and the user_id of the ad creater is got from the authentication token passed in request header to make sure the ad was created by him/her. Would this make more sense to be api/users/authenticated_user/ads/{ad_id}
Delete an ad - DELETE api/ads/{ad_id} -
The user_id of the ad creater is got from the authentication token passed in request header to make sure the ad was created by him/her. Would this make more sense to be api/users/authenticated_user/ads/{ad_id}
Get a users ads by id - GET /api/users/{user_id}/ads
Get logged in users ads - GET /api/users/authenticated_user/ads -
身份验证令牌在请求标头中传递,用于在数据库中查找用户。
在某些端点中使用身份验证令牌的原因是因为客户端无法访问登录用户的 user_id,只能访问身份验证令牌。 谢谢,非常感谢您的意见。
【问题讨论】:
-
这将倾向于意见。嵌入在路线信息中的问题基本上是不可见的,除非有人碰巧滚动,顺便说一句——虽然它不会改变问题的性质,但如果页面边缘有问题,人们可能看不到它们。跨度>
-
@DaveNewton 谢谢,我在那里改变了它,希望它更具可读性。我知道在处理 REST URI 时,它可能有点基于意见,但这是我在可接受范围内所拥有的。谢谢
标签: rest web-applications