我正在使用API,希望我按以下格式执行请求:
?filter=value1&filter=value2
但是,我正在使用Active Resource,当我指定:params 哈希时,我无法使相同的参数在URL中出现两次,我认为这是正确的。所以我不能这样做:
:params => {:consumer_id => self.id, :filter => "value1", :filter => "value2" }
,因为哈希的第二个过滤器索引将被忽略。
我知道我可以传递一个数组(我认为这是正确的方法),如下所示:
:params => {:consumer_id => self.id, :filter => ["value1","value2"] }
这会生成如下的网址:
?filter[]=value1&filter[]=value2
对我来说似乎没问题,但API不接受它。所以我的问题是:
使用多个值传递参数的正确方法是什么?它是语言特定的吗?谁决定这个?
答案 0 :(得分:6)
http://guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters
尝试:filter [] => value,:filter [] =>值2
答案 1 :(得分:4)
要创建有效的查询字符串,可以使用
params = {a: 1, b: [1,2]}.to_query
http://apidock.com/rails/Hash/to_query
http://apidock.com/rails/Hash/to_param
答案 2 :(得分:0)
您可以通过创建允许重复键的哈希值来生成包含重复参数的查询字符串。 因为这依赖于使用对象ID作为哈希键,所以至少对于重复的键,您需要使用字符串而不是符号。
(参考:Ruby Hash with duplicate keys?)
GET /videos/modelresult/_search?_source=true [status:400 request:0.001s]
Failed to query Elasticsearch using '(video)': TransportError(400, 'parsing_exception')
Traceback (most recent call last):
File "/home/user-name/miniconda3/envs/project-web/lib/python3.7/site-packages/haystack/backends/elasticsearch_backend.py", line 524, in search
_source=True)
File "/home/user-name/miniconda3/envs/project-web/lib/python3.7/site-packages/elasticsearch/client/utils.py", line 69, in _wrapped
return func(*args, params=params, **kwargs)
File "/home/user-name/miniconda3/envs/project-web/lib/python3.7/site-packages/elasticsearch/client/__init__.py", line 527, in search
doc_type, '_search'), params=params, body=body)
File "/home/user-name/miniconda3/envs/project-web/lib/python3.7/site-packages/elasticsearch/transport.py", line 307, in perform_request
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
File "/home/user-name/miniconda3/envs/project-web/lib/python3.7/site-packages/elasticsearch/connection/http_urllib3.py", line 93, in perform_request
self._raise_error(response.status, raw_data)
File "/home/user-name/miniconda3/envs/project-web/lib/python3.7/site-packages/elasticsearch/connection/base.py", line 105, in _raise_error
raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.RequestError: TransportError(400, 'parsing_exception')