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

前端之家收集整理的这篇文章主要介绍了xml – REST – 何时使用400(“不良请求”)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个像这样的销售/客户/ {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是适当的;但是,您可能还有其他选项。

猜你在找的XML相关文章