Bugs item #10340, was opened at 2007-04-25 00: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. :)

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

Comment By: Minero Aoki (aamine)
Date: 2007-05-06 19:02

Message:
Thank you for your report.  I commited my patch which allows you to give form parameters by an array of strings (of course, a string is allowed, too).  It goes to 1.8 branch after short verification term.

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

Comment By: Scott Archer (scarcher2)
Date: 2007-05-04 22:54

Message:
Index: lib/net/http.rb
===================================================================
--- lib/net/http.rb	(revision 12246)
+++ lib/net/http.rb	(working copy)
@@ -1454,7 +1454,10 @@
     # This method also set Content-Type: header field to
     # application/x-www-form-urlencoded.
     def set_form_data(params, sep = '&')
-      self.body = params.map {|k,v| "#{urlencode(k.to_s)}=#{urlencode(v.to_s)}" }.join(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
 

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

Comment By: Colin Curtin (perplexes)
Date: 2007-05-01 00:27

Message:
I contacted the maintainer of this, Minero Aoki
<aamine / loveruby.net>, but have not received a response yet.

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

Comment By: Scott Archer (scarcher2)
Date: 2007-04-29 03:49

Message:
I also had this same problem.
I submitted a patch to the ruby core mailing list.

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/11052

My solution wasn't as concise as yours (I'm new to ruby) but it does the exact same thing.
I'm still trying to figure out how to participate in the community.

I'd like to see this bug fixed, and the fix doesn't break any of the unit tests.


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

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