Passing arrays using Net-http in Ruby

Hi all,
We needed to use net/http of ruby for sending parameters between two modules.
We decided to use the get and the post request of http as follows:
url = URI.parse(“http://SERVER:PORT”)
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Get.new(“/method_name?parameter=a”)
response = http.request(request)
In the above get request the parameter “a” is taken to the “http://SERVER:PORT/method_name” and data related to it is queried.
url = URI.parse(“http://SERVER:PORT”)
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Post.new(“/method_name”)
request.set_form_data({“parameter1” => “a”, “parameter2” => “b”, “parameter3” => “c” })
response = http.request(request)
In the above post request three parameters are taken parameter1, parameter2 and parameter3  with the value a,b and c respectively to the “http://SERVER:PORT/method_name” for posting.
When you need to pass a array in the parameters for posting. you can specify it as:
request.set_form_data({“parameter1” => “a”, “parameter2” => [“1”, “2”, “3”], “parameter3” => “c” })
Here the parameter2 takes a array with value 1,2,3.
Now the fun begins,
If you are using ruby 1.9.2 this works fine parameter2 is passed as a array.(parameter2 = [“1”, “2”, “3”])
However if you are uing ruby 1.8.7 this doesnot work, parameter2 appends all the values of the array to form  a string and passes it.(parameter2 = “123”)
You have to override the set_form_data method for it to work and pass parameter as a array.
It can be done as below. You an place this in a file and let the file be included while loading the app.
module Net
module HTTPHeader
def set_form_data(request, params, sep = ‘&’)
request.body = params.map {|k,v|
if v.instance_of?(Array)
v.map {|e| “#{urlencode(k.to_s)}=#{urlencode(e.to_s)}”}.join(sep)
else
“#{urlencode(k.to_s)}=#{urlencode(v.to_s)}”
end
}.join(sep)
request.content_type = ‘application/x-www-form-urlencoded’
end
alias form_data= set_form_data
def urlencode(str)
str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf(‘%%%02x’, s[0]) }
end
end
end
And then use set_form data as follows:
request.set_form_data(request, {“parameter1” => “a”, “parameter2” => [“1”, “2”, “3”], “parameter3” => “c” })
Thus we can pass a array in parameters while use net/http of ruby 1.8.7.
References : http://blog.assimov.net/post/653645115/post-put-arrays-with-ruby-net-http-set-form-data

In one of our projects, we needed to use net/http of ruby for sending parameters between two modules which resided on two different servers.

We decided to use the get and the post request of http as follows:

url = URI.parse("http://SERVER:PORT")

http = Net::HTTP.new(url.host, url.port)

request = Net::HTTP::Get.new("/method_name?parameter=a")

response = http.request(request) 

In the above get request the parameter “a” is send to the “http://SERVER:PORT/method_name” and data related to it is queried.

url = URI.parse("http://SERVER:PORT")

http = Net::HTTP.new(url.host, url.port)

request = Net::HTTP::Post.new("/method_name")

request.set_form_data({"parameter1" => "a", "parameter2" => "b", "parameter3" => "c" })

response = http.request(request) 

In the above post request three parameters are taken parameter1, parameter2 and parameter3 with the value a,b and c respectively to the “http://SERVER:PORT/method_name” for posting.

When you need to pass a array in the parameters for posting. you can specify it as:

request.set_form_data({"parameter1" => "a", "parameter2" => ["1", "2", "3"], "parameter3" => "c" }) 

Here the parameter2 takes a array with value 1,2,3.

Now the fun begins,

If you are using ruby 1.9.2 this works fine parameter2 is passed as a array.(parameter2 = [“1”, “2”, “3”])

However if you are uing ruby 1.8.7 this doesnot work, parameter2 appends all the values of the array to form  a string and passes it.(parameter2 = “123”)

You have to override the set_form_data method for it to work and pass parameter as a array.

It can be done as below. You an place this in a file and let the file be included while loading the app.

module Net

module HTTPHeader

def set_form_data(request, params, sep = '&')

request.body = params.map {|k,v|

if v.instance_of?(Array)

v.map {|e| "#{urlencode(k.to_s)}=#{urlencode(e.to_s)}"}.join(sep)

else

"#{urlencode(k.to_s)}=#{urlencode(v.to_s)}"

end

}.join(sep)

request.content_type = 'application/x-www-form-urlencoded'

end

alias form_data= set_form_data

def urlencode(str)

str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }

end

end

end 

And then use set_form data as follows:

 request.set_form_data(request, {"parameter1" => "a", "parameter2" => ["1", "2", "3"], "parameter3" => "c" }) 

Thus we can pass a array in parameters while use net/http of ruby 1.8.7.

If any other solutions please feel free to share.

References : http://blog.assimov.net/post/653645115/post-put-arrays-with-ruby-net-http-set-form-data

Leave a reply:

Your email address will not be published.

Site Footer