加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – REST – 何时使用400(“不良请求”)

发布时间:2020-12-16 08:01:00 所属栏目:百科 来源:网络整理
导读:我有一个像这样的销售/客户/ {customerno}的资源。如果客户端向该资源发送PUT请求,则如果实体主体中的xml无效xml,则返回400 – Bad请求。但是,如果xml有效,但xml的内容无效。例如,客户端正在尝试更新客户PostCode,并提供无效的PostCode。在这种情况下
我有一个像这样的销售/客户/ {customerno}的资源。如果客户端向该资源发送PUT请求,则如果实体主体中的xml无效xml,则返回400 – Bad请求。但是,如果xml有效,但xml的内容无效。例如,客户端正在尝试更新客户PostCode,并提供无效的PostCode。在这种情况下返回400 – 错误请求是正确的,还是应该使用另一个http代码?
从 Wikipedia’s List of HTTP Status Codes:

400 Bad Request:
The request cannot be fulfilled due to bad syntax.

在这种情况下,您的客户端向您发送了一个具有无效邮政编码的XML有效载荷,这是一种无效语法的形式;因此,发送一个400错误请求是在这种情况下返回的适当的错误代码。

另外,维基百科引用RFC-4918作为这个主题的资源。从本文档可以看到以下信息:

Servers MAY reject questionable requests (even though
they consist of well-formed XML),for instance,with a 400 (Bad
Request) status code and an optional response body explaining the
problem.

由于您的请求格式正确(XML不错,它只包含语义错误的信息),您可以拒绝带有状态代码400的内容。*可能*表示有其他选项。

虽然您可能会尝试使用状态码422,但在这种情况下,这是不正确的,因为无效的邮政编码不符合语义错误的标准。参见下文…

维基百科:

422 Unprocessable Entity (WebDAV; RFC 4918):
The request was well-formed but was unable to be followed due to semantic errors.

另外,这里有一些definitions to assist in the interpretation of status code 422:

  • Syntax errors occur during the parsing of input code,and are caused by grammatically incorrect statements. Typical errors might be an illegal character in the input,a missing operator,two operators in a row,two statements on the same line with no intervening semicolon,unbalanced parentheses,a misplaced reserved word,etc.

  • Semantic errors occur during the execution of the code,after it has been parsed as grammatically correct. These have to do not with how statements are constructed,but with what they mean. Such things as incorrect variable types or sizes,nonexistent variables,subscripts out of range,and the like,are semantic errors.

您的无效邮政编码既不是语法错误也不是语义错误;因此,排除状态码422作为选项是合理的。

为了回答你的问题,状态代码400是适当的;但是,您可能还有其他选项。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读