REST-何时使用400(“错误请求") [英] REST - When to use 400 ("Bad Request")

查看:430
本文介绍了REST-何时使用400(“错误请求")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似销售/客户/{customerno}的资源.如果客户端向该资源发送PUT请求,则我将返回400-错误请求,如果实体主体中的xml无效xml.但是,如果xml有效,但是xml的内容无效,该怎么办.举例来说,客户正在尝试更新客户的邮政编码,并提供了无效的邮政编码.在这种情况下,返回400-错误的请求是否正确,还是我应该使用的另一个http代码?

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?

推荐答案

来自 Wikipedia的HTTP状态列表代码:

400错误的请求: 由于语法错误,无法满足该请求.

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

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

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.

此外,Wikipedia引用 RFC-4918 作为该主题的资源.在此文档中,您将找到以下信息:

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

服务器可能会拒绝有问题的请求(即使 它们包含格式良好的XML),例如,包含400(错误 请求)状态代码和可选的响应正文,以解释 问题.

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的内容.单词*may*表示存在还有其他选择.

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

尽管您可能会想使用状态码422,但在这种情况下这是不正确的,因为无效的邮政编码不符合语义错误的标准.阅读下面的内容...

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不可处理的实体(WebDAV; RFC 4918): 该请求格式正确,但由于语义错误而无法遵循.

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

此外,这是一些定义,以帮助解释状态码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作为选项是合理的.

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.

要回答您的问题,请使用状态码400.但是,您可能还有其他选择.

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

这篇关于REST-何时使用400(“错误请求")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆