Ruby的法拉第 - 包括相同的参数多次 [英] Ruby's Faraday - include the same param multiple times
问题描述
我正在针对迫使我相同参数的名称多次发送给不同的级联过滤标准的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
我使用的是法拉第我的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.
推荐答案
我一直在试图找出如何,现在一段时间做到这一点。我最终通过法拉第code挖掘,发现正好在测试套件这个用例。
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屋!