错误数据的 HTTP 状态代码
当客户端发布错误数据时,我应该返回什么 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这正是 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.
如果语法错误,请使用“400 Bad Request”
如果数据错误(且语法正确),请使用“422 Unprocessable Entity”
请参阅 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"
In case of bad data (and correct syntax) use "422 Unprocessable Entity"
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.
当客户端点击提交按钮时,我真的更倾向于将不良数据捕获回浏览器中。
如果没有,那么我会返回 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: