如果请求缺少必需参数,我应该使用什么HTTP状态响应代码? [英] What HTTP status response code should I use if the request is missing a required parameter?

查看:2158
本文介绍了如果请求缺少必需参数,我应该使用什么HTTP状态响应代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在考虑412(前置条件失败),但可能有更好的标准?

I am thinking 412 (Precondition Failed) but there may be a better standard?

推荐答案

状态422似乎最合适在规范上。

Status 422 seems most appropiate based on the spec.

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.

他们声称格式错误的xml是一个错误语法的例子(调用400)。格式错误的查询字符串似乎与此类似,因此400似乎不适合缺少参数的格式良好的查询字符串。

They state that malformed xml is an example of bad syntax (calling for a 400). A malformed query string seems analogous to this, so 400 doesn't seem appropriate for a well-formed query-string which is missing a param.

UPDATE @DavidV正确指出此规范适用于WebDAV,而非核心HTTP。但是由于缺乏更好的状态代码,一些流行的非WebDAV API无论如何都在使用422(看到这个)。

UPDATE @DavidV correctly points out that this spec is for WebDAV, not core HTTP. But some popular non-WebDAV APIs are using 422 anyway, for lack of a better status code (see this).

这篇关于如果请求缺少必需参数,我应该使用什么HTTP状态响应代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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