用于验证资源的 REST 实践 [英] REST practices for validating a resource
问题描述
我目前正在实施令牌"REST 服务.
I am currently implementing a "token" REST service.
令牌只是一个字符串,由一些参数构建而成,然后经过哈希处理并在一定时间后过期.
A token is just a string, built from some parameters, which is then hashed and expires after a certain amount of time.
我想在我的 REST 服务中有一个可以验证令牌的端点,但我不是 100% 确定如何以 RESTful 方式实现它
I want to have an endpoint in my REST service which can validate a token, but I'm not 100% sure how to implement it in a RESTful way
- 我认为这应该通过 GET 来完成,因为它不会改变状态,只要我正确设置了缓存控件,它就可以缓存一段合理的时间.即
mysite.com/token/kjfhwekjfwekj
- 什么是适当的返回代码?如果有效,我会假设为 200,但如果无效呢?我觉得 400 是错误的,因为虽然资源本身无效,但客户端并没有错误地调用端点.这里的 404 正确吗?如果我们将代币视为短期资源,我猜是这样?
推荐答案
GET
是检查令牌的正确 HTTP 动词.
GET
is the correct HTTP verb to check a token.
假设令牌的无效"意味着:
Assuming that 'invalid' for a token means that:
- 它从未存在过,或者
- 它曾经存在但不再存在.
您希望您的服务的客户能够了解区别吗?
Do you want the client of your service to be able to know the difference?
- 如果是,则为案例 1 返回
404 Not Found
,为案例 2 返回410 Gone
. - 如果否,则返回
404 Not Found
.
- If yes, return
404 Not Found
for case 1, and410 Gone
for case 2. - If no, return
404 Not Found
.
如果token存在,200 OK
是正确的.
If the token exists, 200 OK
is correct.
这篇关于用于验证资源的 REST 实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!