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是适当的;但是,您可能还有其他选项。

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念