post 违反要求的 HTTP 403 或 406?

HTTP 403 or 406 for post requirements violation?

简单问题:

我有一个 POST 端点需要一些文本输入。输入的文本不得超过 x 个字符。如果是,我会回复一条错误消息。

但是哪个 HTTP 状态代码是正确的?

0.4.4 403 Forbidden

The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated. [..]

10.4.7 406 Not Acceptable

The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.

10.4.18 417 Expectation Failed

The expectation given in an Expect request-header field [..] could not be met by this server [..]

Source: w3.org

我猜这是存在的问题。 很快正确的 HTTP 代码是 400 + 可选描述.

您可以在此处查看更详细的答案: