validation - 错误输入应该使用什么 HTTP 状态代码

标签 validation http

当不报告 200(一切正常)但输入错误时,最佳 HTTP 响应代码是什么?

比如,你向服务器提交一些数据,它会响应你的数据有误

使用 500 看起来更像是服务器问题
200 与警告/错误响应文本一起使用是错误的(允许缓存但一切都不正常)
使用 204 并且不返回任何内容,这可能很好(但得到很好的支持?)
如果请求的路径(脚本)可用且位置正确,则使用 404 是错误的

最佳答案

我们在制作 API 时也遇到了同样的问题。我们正在寻找等同于 InvalidArgumentException 的 HTTP 状态代码。阅读下面的源文章后,我们最终使用了 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.

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

关于validation - 错误输入应该使用什么 HTTP 状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7939137/

相关文章:

c# - 使用线程的 ASP.NET MVC 自定义验证

validation - Phalcon 框架 : Can't save not required strings in database

Javascript - 从不同国家发送单个http请求

java - 线程限制超出替换阻塞的工作线程

php - 是否可以仅发送http请求以获取最后修改时间?

C http服务器编程

Python 3 - 如何验证多个用户输入

javascript - 具有非法列名的数据文件,读取标题作为键

在 Linux/MAC 上无法识别 http 响应中的 JSON 编码的 unicode 但在 Windows 上?

python - (Python 2.7)使用 try 进行数据验证无法按预期工作