Ruby 的法拉第 - 多次包含相同的参数 [英] Ruby's Faraday - include the same param multiple times

查看:20
本文介绍了Ruby 的法拉第 - 多次包含相同的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 API,该 API 迫使我多次发送相同参数的名称以级联不同的过滤条件.因此,示例 api GET 调用如下所示:

I'm working against an API that forces me to send the same parameter's name multiple times to cascade different filtering criteria. So a sample api GET call looks like this:

GET http://api.site.com/search?a=b1&a=b2&a=b3&a=c2

我将 Faraday 用于我的 REST 适配器,它将其 URL 参数作为哈希(因此 - 它具有唯一键).这意味着我不能做这样的事情:

I'm using Faraday for my REST adapter, which takes its URL parameters as a Hash (therefore - it has unique keys). This means I cannot do something like this:

response = Faraday.new({
  url: 'http://api.site.com/search'
  params: { a: 'b1', a: 'b2', a: 'b3', a: 'c2' } # => nay
}).get

我试图在发送请求之前破解网址:

I tried to hack on the url right before the request is sent:

connection = Faraday.new(url: 'http://api.site.com/search')
url        = connection.url_prefix.to_s
full_url   = "#{ url }?a=b1&a=b2&a=b3&a=c2"
response   = connection.get( full_url )

这不起作用 - 当我调试响应时,我看到发送到 API 服务器的实际 url 是:

Which did not work - when I debug the response I see that the actual url sent to the API server is:

GET http://api.site.com/search?a[]=b1&a[]=b2&a[]=b3&a[]=c2

我无法更改 API.有没有办法继续与法拉第合作并以优雅的方式解决这个问题?谢谢.

I have no way to change the API. Is there a way to keep working with Faraday and solve this in an elegant way? thanks.

推荐答案

一段时间以来,我一直试图弄清楚如何做到这一点.我最终深入研究了法拉第代码,并在测试套件中找到了这个用例.

I've been trying to figure out how to do this for a while now. I ended up digging through the Faraday code, and found exactly this use case in the test suite.

:params => {:color => ['red', 'blue']}

应该屈服

color=red&color=blue

这篇关于Ruby 的法拉第 - 多次包含相同的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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