逻辑错误的 HTTP 400(错误请求),而不是格式错误的请求语法

标签 http http-status-codes http-status-code-400

HTTP/1.1 specification (RFC 2616)关于status code 400, Bad Request (§10.4.1)的含义有以下说法:

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

如今,在一些基于 HTTP 的 API 中似乎有一种通用做法,即使用 400 来表示请求的逻辑错误,而不是语法错误。我的猜测是 API 这样做是为了区分 400。 (客户引起的)和500 (服务器引起的)。使用 400 来指示非语法错误是可以接受的还是不正确的?如果可以接受,是否有关于 RFC 2616 的带注释的引用资料可以更深入地了解 400 的预期用途?

例子:

最佳答案

想到状态 422 ( RFC 4918, Section 11.2):

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.

关于逻辑错误的 HTTP 400(错误请求),而不是格式错误的请求语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4781187/

相关文章:

javascript - 从具有特定 http 状态的 express 中间件退出

android - POST 请求给出 FileNotFoundException

WCF 400 错误请求

java - HttpURLConnection GET 请求得到 400 Bad Request

javascript - 重复有效负载(数据)如何减少网络套接字中的延迟?

ruby-on-rails - Apache设置多个站点

Heroku 中带有自定义域的 HTTPS

java - 如何通过 HTTP 调用支持 SSO 的服务

c# - 以编程方式检查 IIS7 启动画面

rest - 如果 POST 成功但没有创建任何新内容,我们应该返回什么状态代码?