错误数据的 HTTP 状态代码

发布于 2024-08-03 18:53:43 字数 180 浏览 2 评论 0原文

当客户端发布错误数据时,我应该返回什么 HTTP 状态代码(例如,需要整数时返回字符串)?

我一直在使用 400 Bad Request,但当我阅读 HTTP 文档时,它似乎更适用于 HTTP 协议错误。

我想使用状态代码,以便 Flash 和 AJAX 客户端可以区分成功、错误数据和服务器错误,而无需解析响应。

What HTTP status code should I return when a client posts bad data (e.g. a string when integer was expected)?

I've been using 400 Bad Request, but as I read over the HTTP docs that seems more applicable to HTTP protocol errors.

I'd like to use a status code so that Flash and AJAX clients can distinguish between success, bad data, and server error without having to parse a response.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁吢 2024-08-10 18:53:43

这正是 400 的用途。是的,它用于不良的 HTTP 协议使用,但它不仅仅用于此目的。

This is exactly what 400 is for. Yes, it's used for bad HTTP protocol usage, but it's not exclusively for that purpose.

〗斷ホ乔殘χμё〖 2024-08-10 18:53:43

如果语法错误,请使用“400 Bad Request”

由于语法错误,服务器无法理解该请求。客户端不应在未经修改的情况下重复请求。

如果数据错误(且语法正确),请使用“422 Unprocessable Entity”

422(不可处理的实体)状态代码意味着服务器理解请求实体的内容类型(因此 415(不支持的媒体类型)状态代码是不合适的),并且请求实体的语法是正确的(因此 400 (错误请求)状态代码不合适)但无法处理所包含的指令。例如,如果 XML 请求主体包含格式正确(即语法正确)但语义错误的 XML 指令,则可能会出现此错误情况。

请参阅 https:// www.bennadel.com/blog/2434-http-status-codes-for-invalid-data-400-vs-422.htm

另请参阅https://softwareengineering.stackexchange.com/a/342896/158699答案,具有正确的400和422代码。

In case of bad syntax use "400 Bad Request"

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

In case of bad data (and correct syntax) use "422 Unprocessable Entity"

The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415 (Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.

See https://www.bennadel.com/blog/2434-http-status-codes-for-invalid-data-400-vs-422.htm

See also https://softwareengineering.stackexchange.com/a/342896/158699 answer, with correct both 400 and 422 code.

〗斷ホ乔殘χμё〖 2024-08-10 18:53:43

当客户端点击提交按钮时,我真的更倾向于将不良数据捕获回浏览器中。

如果没有,那么我会返回 400,因为正如标准所说:

由于语法错误,服务器无法理解该请求。客户端不应在未经修改的情况下重复请求。

I'd really be more inclined to trap the bad data back in the browser when the client hits the submit button.

If not, then I'd return 400 because as the standard says:

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文