Bugs item #10340, was opened at 2007-04-24 15:49
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=1698&aid=10340&group_id=426

Category: Standard Library
Group: 1.8.5
Status: Open
Resolution: None
Priority: 3
Submitted By: Colin Curtin (perplexes)
Assigned to: Nobody (None)
Summary: Net::HTTP encodes parameters improperly.

Initial Comment:
While trying to post a web form with lots of checkboxes, I ran into the interesting problem that Hash's with arrays for values, when doing a Net::HTTP.post_form, does not work properly.

To illustrate:

"
require 'net/http'
require 'uri'

uri = URI.parse('http://ihateosx.com/~colin/post_test.php')
data = {'test[]' => ['one', 'two', 'three']}

response = Net::HTTP.post_form(uri, data)
print response.body
"

Results in: 

array(1) {
  ["test"]=>
  array(1) {
    [0]=>
    string(11) "onetwothree"
  }
}
=> nil

When we look around in net/http.rb, the problem becomes obvious:

def set_form_data(params, sep = '&')
      self.body = params.map {|k,v| "#{urlencode(k.to_s)}=#{urlencode(v.to_s)}" }.join(sep)
      self.content_type = 'application/x-www-form-urlencoded'
end

The method does not allow for arrays to be values in the params Hash. Array.to_s only appends strings together.

Without doing much research, I turn it into:

def set_form_data(params, sep = '&')
      params_array = params.map do |k,v| 
        v.inject([]){|c, val| c << "#{urlencode(k.to_s)}=#{urlencode(val.to_s)}"}.join(sep)
      end
      self.body = params_array.join(sep)
      self.content_type = 'application/x-www-form-urlencoded'
end

Which will take something like
 {'test[]' => ['one', 'two', 'three']}

and turn it into

test[]=one&test[]=two&test[]=three

Which is correct for array post parameters. And indeed the PHP test page shows it also:

array(1) {
  ["test"]=>
  array(3) {
    [0]=>
    string(3) "one"
    [1]=>
    string(3) "two"
    [2]=>
    string(5) "three"
  }
}
=> nil


So, what to do from here? This is my first Ruby bug report. Please be gentle. :)

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=1698&aid=10340&group_id=426