Browse files

passing array of params in :parameters parameter

  • Loading branch information...
1 parent c9737b2 commit f6341464240b75715aeac04ed3bc4b71d8348607 @mustafaturan mustafaturan committed Aug 12, 2012
Showing with 15 additions and 2 deletions.
  1. +7 −2 lib/http_request.rb
  2. +8 −0 test/test_http_request.rb
View
9 lib/http_request.rb
@@ -298,8 +298,13 @@ def parse_options(method, options)
# parse parameters for the options[:parameters] and @uri.query
def parse_parameters
if @options[:parameters].is_a?(Hash)
- @options[:parameters] = @options[:parameters].collect{|k, v|
- "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
+ @options[:parameters] = @options[:parameters].collect{|k, v|
+ unless v.is_a?(Array)
+ "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
+ else
+ prefix = "#{k}[]"
+ v.collect { |value| "#{CGI.escape(k.to_s)}=#{CGI.escape(value.to_s)}" }.join('&')
+ end
}.join('&')
end
@options[:parameters] = '' if @options[:parameters].nil?
View
8 test/test_http_request.rb
@@ -89,6 +89,10 @@
'ids' => ['1', '2']
}.inspect)
+ hr.get(:url => URL + '/get', :parameters => {:ids => ['1', '2']}).body.should.equal({
+ 'ids' => ['1', '2']
+ }.inspect)
+
hr.get(URL + '/get?ids[a]=1&ids[b]=2').body.should.equal({
'ids' => {'a' => '1', 'b' => '2'}
}.inspect)
@@ -133,6 +137,10 @@
hr.post(:url => URL + '/get', :parameters => 'ids[]=1&ids[]=2').body.should.equal({
'ids' => ['1', '2']
}.inspect)
+
+ hr.post(:url => URL + '/get', :parameters => {:ids => ['1','2']}).body.should.equal({
+ 'ids' => ['1', '2']
+ }.inspect)
hr.post(URL + '/get?ids[a]=1&ids[b]=2').body.should.equal({
'ids' => {'a' => '1', 'b' => '2'}

0 comments on commit f634146

Please sign in to comment.