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

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

问题描述

我在想 412(前提条件失败)但可能有更好的标准?

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

推荐答案

Status 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天全站免登陆