# Net::Http multiple parameters with the same name

**URL:** <https://www.ruby-forum.com/t/net-http-multiple-parameters-with-the-same-name/92948>\
**Category:** Ruby\
**Created:** [April 26, 2007, 12:01am UTC](https://www.ruby-forum.com/t/net-http-multiple-parameters-with-the-same-name/92948 "2007-04-26T00:01:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scott\_Archer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Scott\_Archer](https://www.ruby-forum.com/u/Scott_Archer)\
**Post date:** [April 26, 2007, 12:01am UTC](https://www.ruby-forum.com/t/net-http-multiple-parameters-with-the-same-name/92948/1 "2007-04-26T00:01:36Z")

</div>

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.

test=1  
test=2  
test=3

if I were submitting the form in html it would look something like this.

This is the ruby code I am using currently.

url = URI.parse(“[http://localhost/test/test.servlet](http://localhost/test/test.servlet)”)  
req = Net::HTTP::Post.new(url.path)  
req.set\_form\_data({‘test’=\>[‘1’,‘2’]})  
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)  
}  
case res  
when Net::HTTPSuccess, Net::HTTPRedirection  
xml = res.body  
else  
res.error!  
end  
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  
across.

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

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/246557](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/246557)

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![Scott\_Archer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Scott\_Archer](https://www.ruby-forum.com/u/Scott_Archer)\
**Post date:** [April 27, 2007, 1:32am UTC](https://www.ruby-forum.com/t/net-http-multiple-parameters-with-the-same-name/92948/2 "2007-04-27T01:32:50Z")

</div>

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  
process.

# 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 = []
  params.map do |k,v|
    if(v.is_a? Array)
      for value in v
        temp << "#{urlencode(k.to_s)}=#{urlencode(value.to_s)}"
      end
    else
      temp << "#{urlencode(k.to_s)}=#{urlencode(v.to_s)}"
    end
  end
  self.body = temp.join(sep) #= params.map {|k,v|

```

“#{urlencode(k.to\_s)}=#{urlencode(v.to\_s)}” }.join(sep)  
puts self.body  
self.content\_type = ‘application/x-www-form-urlencoded’  
end
