HTTP 404与400的无效查询参数 [英] HTTP 404 vs 400 for invalid query parameters

查看:96
本文介绍了HTTP 404与400的无效查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的请求网址:

http://server.com/app/user/getuser/?userId= 9999

请注意, userId 是查询参数.没有嵌入的路径参数.

Note that userId is query parameter. Not embedded path parameter.

我了解,如果请求URL为: http://server.com/app/user/getuser/9999 并且ID 9999在数据库中不存在,应使用代码404.

I understand that if the request URL is: http://server.com/app/user/getuser/9999 and the ID 9999 does not exist in database, The code 404 should be used.

userId 是查询参数的情况下应使用什么HTTP状态?现在我返回的是400,而不是404.

BUT what HTTP status should be used for the case userId is query parameter? Right now I am returning 400 instead of 404.

推荐答案

我会使用 404未找到.

RFC 7231 定义了像这样的 400错误请求响应:

400(错误请求)状态码表示服务器由于某些原因(例如格式错误的请求语法,无效的请求消息框架或欺骗性的请求路由)而被视为无法处理或无法处理请求的原因

The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

...由于您的请求有效,并且您正试图访问不存在的资源,因此我认为 404未找到状态更为合适. RFC 7231 定义其含义如下:

...since your request is valid and you are just trying to access a resource that does not exist, I think a 404 Not Found status is more suitable. RFC 7231 defines its meaning like this:

404(未找到)状态码表示原始服务器未找到目标资源的当前表示,或不愿意透露该资源的存在.

The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

这篇关于HTTP 404与400的无效查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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