指示客户端输入未验证的 REST HTTP 响应状态代码?

REST HTTP Response status code to indicate client input is non-verified?

我们有 POST API 提供特定用户的联系方式作为响应。但由于这是机密数据,要访问此详细信息,任何访问它的人都需要输入经过验证的手机号码。

请求看起来像:

POST /api/userdetails
{
   "userid": 123,
   "mobile": "+1394839843"
}

如果 "mobile" 号码不是我们数据库中经过验证的号码,状态代码应该是什么?

What should be status code if "mobile" number is not a verified number in our database?

从一个非常高的层面:想想如果用户提交了一个带有“未验证号码”的表单并执行该操作,您会期望一个普通的旧网站做什么。

Michael Kropat Stop Making It Hard 中的图表将流程分解为更具体的问题。

请求有问题吗?这里显然是肯定的,所以我们立即知道您需要使用 4xx class.

从那里开始,只需浏览 semantics of each code 并了解哪一个最合适。

400 Bad Request 是一般的客户端错误,所以如果找不到更好的匹配项,请使用它。

403 Forbidden 大致是“我理解你的要求,但拒绝采取行动”。这通常与身份验证和 ACL 相关联,但标准实际上并不要求这样做。根据您的描述,这不是一个糟糕的匹配。

422 Unprocessable Entity from WebDav, is another possibility. I'd reject it, on the grounds that the schema of the message body is perfectly correct, the problem is that the specified value isn't aligned with the current state of the server. You might want to review a previous discussion about 400 vs 422