用于验证资源的 REST 实践 [英] REST practices for validating a resource

查看:47
本文介绍了用于验证资源的 REST 实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在实施令牌"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

  1. 我认为这应该通过 GET 来完成,因为它不会改变状态,只要我正确设置了缓存控件,它就可以缓存一段合理的时间.即 mysite.com/token/kjfhwekjfwekj
  2. 什么是适当的返回代码?如果有效,我会假设为 200,但如果无效呢?我觉得 400 是错误的,因为虽然资源本身无效,但客户端并没有错误地调用端点.这里的 404 正确吗?如果我们将代币视为短期资源,我猜是这样?

推荐答案

GET 是检查令牌的正确 HTTP 动词.

GET is the correct HTTP verb to check a token.

假设令牌的无效"意味着:

Assuming that 'invalid' for a token means that:

  1. 它从未存在过,或者
  2. 它曾经存在但不再存在.

您希望您的服务的客户能够了解区别吗?

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, and 410 Gone for case 2.
  • If no, return 404 Not Found.

如果token存在,200 OK是正确的.

If the token exists, 200 OK is correct.

这篇关于用于验证资源的 REST 实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆