RESTful API:需要用户代理字符串吗?
我的团队有一个带有 RESTful API 的网站。我们正在对其进行负载测试,因此我们构建了一个小型控制台应用程序来处理请求。此控制台应用程序不会设置用户代理字符串,这会导致我们的 API 出现错误,因为它是我们数据库中的必填字段。
那么,如果请求中未包含用户代理,我是否应该使 API 更加强大并简单地使用默认字符串(即“未知”)?或者,在这种情况下我应该返回 400 Bad Request 响应吗?我知道这两种方法都是可能的,但我正在寻找标准方法来做到这一点。
My team has a web site with a RESTful API. We're is working on load testing it, so we've built a small console app to hammer it with requests. This console app does not set the user agent string, and that is causing an error in our API because it is a required field in our database.
So, should I make the API extra robust and simply use a default string (i.e. "unknown") if a user agent isn't included in the request? Or, should I return a 400 Bad Request response in this situation? I know either is possible, but I'm looking for the standard way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
User-Agent
标头并非绝对需要出现在 HTTP 请求中(规范说标头应该
存在,而不是必须
),如果你的API能够处理它,它会更加健壮存在。也就是说,对于您的测试应用程序来说,传递“测试应用程序”或其他内容的
User-Agent
标识符可能会很好,这样您就可以在数据库中跟踪它,或者限制或分析您的应用程序。测试流量。不过,我要提醒您不要使用 User-Agent 作为客户端应用程序的明确标识符,因为它很容易被欺骗。听起来你并没有这样做,但我想我应该提一下。
Since the
User-Agent
header is not absolutely required to be present within HTTP requests (the spec says the headerSHOULD
be there, rather thanMUST
), your API would be more robust if it could handle it not being present.That said, it would probably be good for your test app to pass a
User-Agent
identifier of "test app" or something, just so you could track it in your database, or to throttle or profile your test traffic.I would caution you not to use
User-Agent
as a definitive identifier of the client application though, since it is so easily spoofed. It doesn't sound like you're doing that but I thought I'd mention it.