提供带有过滤器的 API 时 Rails 查询字符串参数格式
我正在开放一些 REST API 调用供其他人使用。其中一些包括搜索过滤器。
假设我有一个杂货端点,当我当前拨打电话时,我可能会使用:
/grocery_items/index.json?types[]=fruit&types[]=deli
让我留下 params[:types] 作为一个很好的集合。
然而,为了让使用我的 API 的人们更容易,我希望能够提供这样的东西:
/grocery_items/index.json?types=fruit,deli
将我的参数分成一个集合似乎很简单,但我想知道是否存在陷阱,因为这似乎违背了Rails 如何期望集合作为参数到达。
I'm opening up a few REST API calls to others to use. Some of them include search filters.
Let's say I have a grocery endpoint When I currently make calls I might use:
/grocery_items/index.json?types[]=fruit&types[]=deli
Leaving me with params[:types] as a nice collection.
However to make things easier for the folks consuming my API I want to be able to offer something like this:
/grocery_items/index.json?types=fruit,deli
Seems trivial to just split my params into a collection but I'm wondering if there are pitfalls since this seems to be against the grain of how rails expects collections to arrive as params.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为快速执行
params[:types].split(',')
以使调用 API 更易于使用没有任何问题。用查询字符串做一些小把戏是很常见的,这是一个非常温和的改变。I don't see anything wrong with doing a quick
params[:types].split(',')
to make calling your API easier to use. It's pretty common to do tricks with the query string, and this is a really tame change.