"your password did not meet requirements" 的 HTTP 状态代码

HTTP status code for "your password did not meet requirements"

服务器使用什么状态代码来告诉使用站点的 REST API 注册的用户他们的密码不符合指定要求(例如长度)?

根据 this question 的答案,我在考虑 400 或 422。

我的应用程序目前没有前端,完全是一个 API。

What would be the appropriate status code to use for a server telling a user who is signing up using the site's REST API that their password did not meet the specified requirements (such as length)?

状态代码不是为此使用的正确工具

the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition.

消息正文是我们与用户沟通的方式;考虑网络,有关密码不足的信息将在 HTML 消息中发送回浏览器以呈现给用户。

状态码是元数据,它属于通过网络传输文档领域,让通用组件对正在发生的事情有一个标准化的理解,以便他们可以采取适当的行动(例如,状态代码在缓存失效中起着重要作用)。

最直接的选择是 403 Forbidden

The 403 (Forbidden) status code indicates that the server understood the request but refuses to authorize it.

422 Unprocessable Entity 很好 ,特别是因为它将注意力引导到请求的消息正文。

在实践中,这可能并不重要 -- 除了提请注意 HTTP 请求中有问题的部分的次要细节,400403422 可以有效地互换。他们都是Client Error (4xx) class, they are all not cacheable by default的成员,依此类推