HTTP 400(错误请求)用于逻辑错误,而不是格式错误的请求语法 [英] HTTP 400 (bad request) for logical error, not malformed request syntax

查看:2093
本文介绍了HTTP 400(错误请求)用于逻辑错误,而不是格式错误的请求语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTTP / 1.1规范(RFC 2616)对<的含义有以下说法a href =http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1>状态码400,错误请求(第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的预期用途?

There seems to be a general practice among a few HTTP-based APIs these days to use 400 to mean a logical rather than a syntax error with a request. My guess is that APIs are doing this to distinguish between 400 (client-induced) and 500 (server-induced). Is it acceptable or incorrect to use 400 to indicate non-syntactic errors? If it is acceptable, is there an annotated reference on RFC 2616 that provides more insight into the intended use of 400?

示例:

  • Google Data Protocol, Protocol Reference, HTTP Status Codes

推荐答案

状态422( RFC 4918,第11.2节):


422(不可处理的实体)状态代码表示服务器理解请求实体的内容类型(因此415(不支持的媒体类型)状态代码是不合适的),并且请求实体的语法是正确(因此400(错误请求)状态代码不合适)但无法处理包含的指令。例如,如果XML请求主体包含格式正确(即语法正确)但语义错误的XML指令,则可能会出现此错误情况。

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(错误请求)用于逻辑错误,而不是格式错误的请求语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆