如果我的 Json 缺少字段值,return 的 HTTP 错误代码是什么?

What HTTP Error code to return if my Json is missing a field value?

例如,我JSON喜欢:

{
    "firstName":"",
    "lastName":"Gustafson",
    "dob":"1980-09-09T00:00:00.000+00:00"
}

如果这 3 个都是必需的,并且考虑到上面的 Json 正文缺少 firstName 的值,什么 HTTP 错误代码将被认为是适合发送的来自 API?

的客户

400 Bad Request 是否会被视为“错误请求”,即使它格式正确但只是缺少 firstName 的值?

If all 3 of these were required and given that above Json body is missing value for firstName, what HTTP error code would be considered a good fit to sent to clients from an API?

我通常会使用 422 Unprocessable Content

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. For example, this status code can be sent if an XML request content contains well-formed (i.e., syntactically correct), but semantically erroneous XML instructions.

403 Forbidden 也是一个合理的选择。