xml REST - 何时使用 400(“错误请求”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10847224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:30:07  来源:igfitidea点击:

REST - When to use 400 ("Bad Request")

xmlresthttp-status-codesrestful-architecturehttp-status-code-400

提问by rgullhaug

I have a resource like this sales/customers/{customerno}. If a client sends a PUT request to this resource I would return 400 - Bad request if the xml in the entity body is not valid xml. But what if the xml is valid, but the content of the xml is not valid. Say for instance that the client is trying to update the customers PostCode and is providing a PostCode which is not valid. Is it correct to return 400 - Bad request in this case, or is it another http code I should have used?

我有这样的资源 sales/customers/{customerno}。如果客户端向该资源发送 PUT 请求,如果实体正文中的 xml 不是有效的 xml,我将返回 400 - Bad request。但是如果 xml 有效,但是 xml 的内容无效怎么办。例如,假设客户端正在尝试更新客户 PostCode 并提供无效的 PostCode。在这种情况下返回 400 - Bad request 是否正确,还是我应该使用的另一个 http 代码?

回答by jmort253

From Wikipedia's List of HTTP Status Codes:

来自维基百科的 HTTP 状态代码列表

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

400 Bad Request:由于语法错误,请求无法完成。

In this case, your client sent you an XML payload that had an invalid zip code, which is a form of invalid syntax; therefore, sending a 400 Bad Request is an appropriate error code to return in this situation.

在这种情况下,您的客户端向您发送了一个包含无效邮政编码的 XML 负载,这是一种无效语法;因此,发送 400 Bad Request 是在这种情况下返回的适当错误代码。

In addition, Wikipedia cites RFC-4918as a resource on this topic. From this document, you'll find the following information:

此外,维基百科引用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(错误请求)状态代码和一个可选的响应正文来解释问题。

Since your request is well-formed (the XML isn't bad, it just contains semantically incorrect information) you mayreject the content with status code 400. The word *may*suggests that there are other options.

由于您的请求格式良好(XML 不错,它只是包含语义不正确的信息),您可以拒绝状态码为 400 的内容。这个词*may*表明还有其他选项。

While you might be tempted to use status code 422, this would not be correct in this situation, since the invalid zip code does not meet the criteria to be a semantic error. Read below...

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

From Wikipedia:

来自维基百科:

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

422 Unprocessable Entity (WebDAV; RFC 4918):请求格式正确,但由于语义错误而无法遵循。

In addition, here are some definitions to assist in the interpretation of status code 422:

另外,这里有一些定义来帮助解释状态码 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.

  • 语法错误发生在输入代码的解析过程中,是由语法错误的语句引起的。典型的错误可能是输入中的非法字符、缺少运算符、连续两个运算符、同一行中没有中间分号的两个语句、不平衡的括号、错位的保留字等。

  • 在代码被解析为语法正确之后,在代码执行期间会发生语义错误。这些与语句的构造方式无关,而与它们的含义有关。诸如不正确的变量类型或大小、不存在的变量、超出范围的下标等都是语义错误。

Your invalid zip code is neither a syntax error nor a semantic error; thus, it's reasonable to rule out status code 422 as an option.

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

To answer your question, status code 400 is appropriate; however, you may have other options as well.

要回答您的问题,状态码 400 是合适的;但是,您可能还有其他选择。

回答by Darrel Miller

The revised version of the HTTP spec found herehas updated the wording to try and avoid this confusion about 400 being limited to just malformed requests.

此处发现的 HTTP 规范的修订版更新了措辞,以尝试避免这种关于 400 仅限于格式错误的请求的混淆。

7.4.1. 400 Bad Request

The server cannot or will not process the request, due to a client error (e.g., malformed syntax).

7.4.1. 400 错误请求

由于客户端错误(例如,格式错误的语法),服务器无法或不会处理请求。