通过消耗资源有效的API时使用的参数在URL中多个值 - 导轨 [英] Rails - Parameter with multiple values in the URL when consuming an API via Active Resource
问题描述
我的消费我希望做以下格式的请求的API:
I am consuming an API that expects me to do requests in the following format:
?filter=value1&filter=value2
不过,我使用Active资源,当我指定的:PARAMS 散,我做不出来的网址,我相信这是正确的两次出现相同的参数。所以,我不能做到这一点:
However, I am using Active Resource and when I specify the :params hash, I can't make the same parameter to appear twice in the URL, which I believe is correct. So I can't do this:
:PARAMS => {:consumer_id => self.id,:过滤器=> 值1:过滤器=> 值2}
,因为散列的第二个过滤器的索引将被忽略。
:params => {:consumer_id => self.id, :filter => "value1", :filter => "value2" }
, because the second filter index of the hash will be ignored.
我知道我可以传递一个数组(我认为是这样做的正确的方式)是这样的:
I know I can pass an array (which I believe is the correct way of doing it) like this:
:params => {:consumer_id => self.id, :filter => ["value1","value2"] }
这将产生一个URL,如:
Which will produce a URL like:
?filter[]=value1&filter[]=value2
这对我来说似乎确定,但API不接受它。所以我的问题是:
Which to me seems ok, but the API is not accepting it. So my question are:
什么是传递参数具有多个值的正确方法?它是特定语言?谁决定的?
What is the correct way of passing parameters with multiple values? Is it language specific? Who decides this?
推荐答案
要创建一个有效的查询字符串,可以使用
to create a valid query string, you can use
PARAMS = {A:1,B:[1,2]}。to_query
http://apidock.com/rails/Hash/to_query 结果
http://apidock.com/rails/Hash/to_param
这篇关于通过消耗资源有效的API时使用的参数在URL中多个值 - 导轨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!