Bugs: Browse | Submit New | Admin

[#10340] Net::HTTP encodes parameters improperly.

2007-04-24 15:49
Submitted By:
Colin Curtin (perplexes)
Assigned To:
Akinori MUSHA (knu)
Network / Comm / Protocols
Net::HTTP encodes parameters improperly.

Detailed description
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) {
  array(1) {
    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'

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)
      self.body = params_array.join(sep)
      self.content_type = 'application/x-www-form-urlencoded'

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

and turn it into


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

array(1) {
  array(3) {
    string(3) "one"
    string(3) "two"
    string(5) "three"
=> nil

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

Add A Comment: Notepad

Please login


Date: 2007-05-06 10:02
Sender: Minero Aoki

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.
Date: 2007-05-04 13:54
Sender: Scott Archer

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 <<
+      end
+      self.body = params_array.join(sep)
       self.content_type = 'application/x-www-form-urlencoded'
Date: 2007-04-30 15:27
Sender: Colin Curtin

I contacted the maintainer of this, Minero Aoki
<aamine@loveruby.net>, but have not received a response
Date: 2007-04-28 18:49
Sender: Scott Archer

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


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

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

Attached Files:

Name Description Download
No Files Currently Attached


Field Old Value Date By
assigned_tonone2007-06-12 02:25zenspider
category_idMisc / Other Standard Library2007-05-29 20:59zenspider