ruby - 如何让 Ruby 的 RestClient 使用多值查询参数?

标签 ruby rest-client

使用 RestClient gem,我需要创建如下请求:

GET http://host/path?p=1&p=2

完成此操作的正确语法是什么? 请注意,接收主机不是 Rails。

尝试过:

resource = RestClient::Resource.new( 'http://host/path' )
params = { p: '1', p: '2' }  
# ^ Overrides param to have value of 2 (?p=2)

params = { p: ['1','2'] }
# ^ results in 'p[]=abc&p[]=cde' (array [] indicators not wanted)

resource.get( { params: params } )

最佳答案

可以将参数作为字符串传递:

resource.get(params: 'p=1&p=2')

例如,使用restclient shell:

>> RestClient.log = Logger.new(STDOUT)
#<Logger:0x007fa444cbecc0 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007fa444cbec70 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x007fa444cbebd0 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x007fa444cbeba8 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007fa444cbeb58>>>>
>> resource = RestClient::Resource.new( 'http://www.example.net' )
#<RestClient::Resource:0x007fa444c9fdc0 @url="http://www.example.net", @block=nil, @options={}>
>> resource.get(params: 'p=1&p=2')
RestClient.get "http://www.example.net", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Params"=>"p=1&p=2"
# => 200 OK | text/html 1270 bytes
"<!doctype html>\n<html>\n<head>\n    <title>Example Domain</title>\n\n    <meta charset=\"utf-8\" />\n    <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n    <style type=\"text/css\">\n    body {\n        background-color: #f0f0f2;\n        margin: 0;\n        padding: 0;\n        font-family: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n        \n    }\n    div {\n        width: 600px;\n        margin: 5em auto;\n        padding: 50px;\n        background-color: #fff;\n        border-radius: 1em;\n    }\n    a:link, a:visited {\n        color: #38488f;\n        text-decoration: none;\n    }\n    @media (max-width: 700px) {\n        body {\n            background-color: #fff;\n        }\n        div {\n            width: auto;\n            margin: 0 auto;\n            border-radius: 0;\n            padding: 1em;\n        }\n    }\n    </style>    \n</head>\n\n<body>\n<div>\n    <h1>Example Domain</h1>\n    <p>This domain is established to be used for illustrative examples in documents. You may use this\n    domain in examples without prior coordination or asking for permission.</p>\n    <p><a href=\"http://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n"

如果您不想编写代码来构建字符串,您应该避免这样做,因为它不一定简单明了,让 Ruby 的 URI类(class)把它拼凑起来:

require 'uri'
URI::encode_www_form([['p', 1], ['p', 2]])
# => "p=1&p=2"

关于ruby - 如何让 Ruby 的 RestClient 使用多值查询参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26515570/

相关文章:

Ruby:ARGV 打破重音字符

ruby-on-rails - 使用 railsinstaller for windows 在 rails 上安装 ruby

Ruby 模块声明

c# - 使用 Rest 客户端并获取状态代码 406 Not Acceptable

ruby - Ruby 中不同的内存技术

ruby - 如何调试/显示使用 RestClient 发送的请求

ruby-on-rails - 使用 Net::HTTP 向 HTTPS 发送请求

rest - 失眠休息客户端错误

javascript - 将 javascript 添加到 HAML 标记错误