更正请求正文中无效配置的 http 状态?

Correct http status for invalid config in request body?

当 API 接收到映射系统中附加数据但未找到附加数据的属性时,我无法选择正确的 HTTP 状态。我最初在想 422 因为它描述了用例,但听起来它是为 WebDAV 保留的。然后我在想也许 404 但我在心理上将其与 URL 联系起来是不正确的。另一个选项是使用错误代码 200 并显示失败消息。

示例:密钥nvdaKey不是系统知道的密钥配置。

POST: pgpTool.com/encrypt

{
  "message": "my secret message",
  "keyConfigName": "nvdaKey"
}

IANA HTTP Status Code Registry currently lists HTTP Semantics as the authoritative reference for status code 422

The 422 (Unprocessable Content) status code indicates that the server understands the content type of the request content (hence a 415 (Unsupported Media Type) status code is inappropriate), and the syntax of the request content is correct, but was unable to process the contained instructions.

因此,如果您认为这是赢家,那就去吧。

403 Forbidden也是一个选项(“我明白你的要求,但我拒绝履行它”)。

状态代码是 transfer of documents over a network 域中的元数据;目标受众是通用 HTTP 组件(浏览器、缓存、代理......)客户端应该从 body 获取消息的语义(就像我们期望人们阅读网络的方式一样)通过阅读返回的网页来了解错误,而不是通过阅读 HTTP headers).

因此,除了一些纯粹的机械问题(缓存,headers 的解释)之外,只要您获得 class(客户端错误/4xx) 正确。

请注意,不识别 422 的客户端应该将响应视为 400。