在不应该为空的强制参数中传递空值的 http 错误代码应该是什么?

What should be the http error code for passing blank value in a mandatory parameter which should not be blank?

例如:

post方法:

{
  name = "",
  id = 1,
  class = 4
}

name 不应为空白参数,但我仍在 post/put 请求调用中以这种方式发送它。此类数据的输出错误代码应该是什么

许多使用 400 Bad Request。这 used to indicate bad syntax, but the latest HTTP RFC 使其更加广阔。

The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

我更喜欢更具体的 422 Unprocessable Entity。这表明语法没问题,但语义错误。

The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.

从技术上讲,422 来自 WebDAV,而不是 HTTP,但这并不重要。许多 API 使用 422.

两者皆可。

另见 。