Multiple Parameters with Net:Http

S

scott.archer

Is there a way to pass in 2 parameters with the same name to the
Net::Http client in ruby?
It's using a map right now, and it only stores one of the values.
Is this a bug in the Http client implementation?
The http spec allows you to pass in multiple parameters with the same
name.


def get_data(data, in_only='Y')
one = Time.new
url = URI.parse('http://127.0.0.1/ec/data.xml')
req = Net::HTTP::post.new(url.path)
req.basic_auth 'user', 'pass'
req.set_form_data({'test'=>1, 'test'=>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
end
 
R

Robert Klemme

Is there a way to pass in 2 parameters with the same name to the
Net::Http client in ruby?
It's using a map right now, and it only stores one of the values.
Is this a bug in the Http client implementation?
The http spec allows you to pass in multiple parameters with the same
name.


def get_data(data, in_only='Y')
one = Time.new
url = URI.parse('http://127.0.0.1/ec/data.xml')
req = Net::HTTP::post.new(url.path)
req.basic_auth 'user', 'pass'
req.set_form_data({'test'=>1, 'test'=>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
end

Without trying I'd assume that it should work like this:

req.set_form_data({'test'=>[1, 2]})

btw, you can leave out the curlys

req.set_form_data('test'=>[1, 2])

Kind regards

robert
 
S

scott.archer

Is there a way to pass in 2 parameters with the same name to the
Net::Http client in ruby?
It's using a map right now, and it only stores one of the values.
Is this a bug in the Http client implementation?
The http spec allows you to pass in multiple parameters with the same
name.
def get_data(data, in_only='Y')
one = Time.new
url = URI.parse('http://127.0.0.1/ec/data.xml')
req = Net::HTTP::post.new(url.path)
req.basic_auth 'user', 'pass'
req.set_form_data({'test'=>1, 'test'=>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
end

Without trying I'd assume that it should work like this:

req.set_form_data({'test'=>[1, 2]})

btw, you can leave out the curlys

req.set_form_data('test'=>[1, 2])

Kind regards

robert
Here is the ruby program I'm trying to run, and the Java Program that
explains what I am trying to do a little better.



require 'net/http'
url = URI.parse("http://localhost/test/scott.jsp")
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


Here is the java jsp i'm testing it with.

<%
StringBuffer sb = new StringBuffer();
String[] values = request.getParameterValues("test");
sb.append(values.length + " ");
for(int i=0;i<values.length;i++)
sb.append(values + " ");
%>
<%=sb.toString()%>

I would expect values to be an array with 2 items in it.

When I run the program I get back a single value with "12" in it.
instead of 2 separate values with "1" and "2" in them.

The query string in the browser would look like "http://localhost/test/
scott.jsp?test=1&test=2"
But i'd like to do that in ruby with a post request.

Thanks for the help.
 
R

Robert Klemme

Is there a way to pass in 2 parameters with the same name to the
Net::Http client in ruby?
It's using a map right now, and it only stores one of the values.
Is this a bug in the Http client implementation?
The http spec allows you to pass in multiple parameters with the same
name.
def get_data(data, in_only='Y')
one = Time.new
url = URI.parse('http://127.0.0.1/ec/data.xml')
req = Net::HTTP::post.new(url.path)
req.basic_auth 'user', 'pass'
req.set_form_data({'test'=>1, 'test'=>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
end
Without trying I'd assume that it should work like this:

req.set_form_data({'test'=>[1, 2]})

btw, you can leave out the curlys

req.set_form_data('test'=>[1, 2])

Kind regards

robert
Here is the ruby program I'm trying to run, and the Java Program that
explains what I am trying to do a little better.



require 'net/http'
url = URI.parse("http://localhost/test/scott.jsp")
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


Here is the java jsp i'm testing it with.

<%
StringBuffer sb = new StringBuffer();
String[] values = request.getParameterValues("test");
sb.append(values.length + " ");
for(int i=0;i<values.length;i++)
sb.append(values + " ");
%>
<%=sb.toString()%>

I would expect values to be an array with 2 items in it.

When I run the program I get back a single value with "12" in it.
instead of 2 separate values with "1" and "2" in them.

The query string in the browser would look like "http://localhost/test/
scott.jsp?test=1&test=2"
But i'd like to do that in ruby with a post request.

Thanks for the help.


First I'd make sure your JSP actually behaves as expected, i.e. detects
multiple values properly. For example, you could do something like this

<%= Arrays.asList( request.getParameterValues("test") ) %>

This should print a comma separated and bracket enclosed list.

Only then I'd continue to work on the Ruby side.

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top