简体   繁体   English

400 和 422 HTTP 状态码的区别

[英]Difference between 400 and 422 HTTP status codes

There are a lot of discussions if the HTTP request payload is missing mandatory attributes, the response code must either be 400 or 422.如果 HTTP 请求负载缺少必需属性,响应代码必须是 400 或 422,有很多讨论。

I am yet not clear about the difference.我还不清楚其中的区别。

Please suggest with logical scenarios/examples when to use 400 or 422 HTTP status code.请使用逻辑场景/示例建议何时使用 400 或 422 HTTP 状态代码。

I know this question is nearly a year old, but thought I'd add an answer based on my own experience designing and building a very large API domain across nearly 400 microservices.我知道这个问题已经有将近一年的历史了,但我想我会根据我自己在近 400 个微服务中设计和构建一个非常大的 API 域的经验添加一个答案。

Ideally, APIs should follow Postel's Law and be tolerant of what it receives.理想情况下,API 应遵循 Postel 定律并对其接收的内容保持宽容。 This has helped us clarify when to use 400 and when to use 422 .这帮助我们澄清了何时使用400以及何时使用422

In the answer above, we would see the example 400 response as a 422 response.在上面的答案中,我们将示例 400 响应视为 422 响应。 Why?为什么? Because we're tolerant of an "unknown" property by ignoring it.因为我们可以通过忽略它来容忍“未知”属性。 But the required <date> field is missing so we would return a 422 containing an error indicating as a such.但是缺少必需的<date>字段,因此我们将返回一个 422,其中包含一个错误指示。

A 400 response would be raised if the consumer sent through a JSON payload under the application/xml content type or if the XML request was invalid (ie. they had <date>2020-01-07</dat> field).如果消费者通过application/xml内容类型下的 JSON 有效负载发送或 XML 请求无效(即,他们具有<date>2020-01-07</dat>字段),则会引发 400 响应。

An advantage this approach has given us is that we can define a different response contract for 422 responses while allowing 400 responses to remain a fairly generic "WTF are you trying to ask?"这种方法给我们带来的一个优势是,我们可以为 422 个响应定义不同的响应契约,同时允许 400 个响应保持一个相当通用的“你想问的 WTF?” responses.回应。 Our 422 responses contain a collection of error messages that list every request constraint or requirement that has been broken.我们的422个回应包含列出每个请求限制或要求已被打破错误信息的集合

We've also found this approach to be particularly useful for our UX guys who can submit forms to APIs and get back a response with human-readable errors they can map straight back onto the form fields (ie. hitting submit and not having filled in a compulsory field will generate a 422 response with a message stating that property is required).我们还发现这种方法对于我们的 UX 人员特别有用,他们可以向 API 提交表单并返回带有人类可读错误的响应,他们可以直接映射回表单字段(即点击提交但未填写)必填字段将生成 422 响应,其中包含一条消息,说明该属性是必需的)。

As far as I know, 400 is used for syntactially incorrect requests and 422 for semtically incorrect requests.据我所知,400 用于语法错误的请求,422 用于语义错误的请求。 If you expect a Request like如果您期望像这样的请求

...
  <name>test name</name>
  <date>2020-01-07</date>
...

400 would be: 400 将是:

...
  <name>test name</name>
  <dateString>2020-01-07</dateString>
...

422 would be: 422 将是:

...
  <name>test name</name>
  <date>hello</date>
...

I only use 400, because 422 is not defined in HTTP/1.1 RFC7231我只使用 400,因为HTTP/1.1 RFC7231 中没有定义 422

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM