RESTful API: require user agent string?

我的未来我决定 提交于 2019-12-10 12:39:59

问题


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.


回答1:


Since the User-Agent header is not absolutely required to be present within HTTP requests (the spec says the header SHOULD be there, rather than MUST), 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.



来源:https://stackoverflow.com/questions/6255162/restful-api-require-user-agent-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!