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/