Forum: Ruby Net::Http multiple parameters with the same name.

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Scott Archer (Guest)
on 2007-04-26 02:01
(Received via mailing list)
I'm trying to use the Net::Http libraries to do a post request to a
java servlet.
I need to submit multiple parameters with the same name.


if I were submitting the form in html it would look something like this.
<form method="POST" action="test.servlet">
<input type="hidden" name="test" value="1" />
<input type="hidden" name="test" value="2" />
<input type="hidden" name="test" value="3" />
<input type="submit" name="submit">

This is the ruby code I am using currently.

url = URI.parse("http://localhost/test/test.servlet")
req =
res =, url.port).start {|http| http.request(req)
    case res
    when Net::HTTPSuccess, Net::HTTPRedirection
      xml = res.body
puts xml

In the servlet I'm doing the following
String[] values = request.getParameterValues("test");
This should return an array with the String values ["1","2","3"]
It's returning an array with one value ["123"]

I know the java code is correct. I'm trying to migrate an existing
program to ruby, however this is the only stumbling block I've come

I found this reference on the mailing list but there were no replies.

Any help would be appreciated.
Scott Archer (Guest)
on 2007-04-27 03:32
(Received via mailing list)
I updated a method in http.rb
It seems to have fixed the problem I was having.

There may be a more elegant solution. I'd love for some criticism if
someone wants to look at this.

I'd also be interested in getting this code into the ruby distribution
if it looks good. I'm new to ruby, and I don't know much about the

# Set header fields and a body from HTML form data.
    # +params+ should be a Hash containing HTML form data.
    # Optional argument +sep+ means data record separator.
    # This method also set Content-Type: header field to
    # application/x-www-form-urlencoded.
    def set_form_data(params, sep = '&')
      temp = [] do |k,v|
        if(v.is_a? Array)
          for value in v
            temp << "#{urlencode(k.to_s)}=#{urlencode(value.to_s)}"
          temp << "#{urlencode(k.to_s)}=#{urlencode(v.to_s)}"
      self.body = temp.join(sep) #= {|k,v|
"#{urlencode(k.to_s)}=#{urlencode(v.to_s)}" }.join(sep)
      puts self.body
      self.content_type = 'application/x-www-form-urlencoded'
This topic is locked and can not be replied to.