I am doing an http get using the url http://localhost/add?add_key[0][key]=1234&add_key[0][id]=1. I have a rails app which gives me a neat params hash {"add_key"=>{"0"=>{"key"=>"1234", "id"=>"1"}}. Now I try to post these to another server, using Net::HTTP.post_form new_url,params The second server gets a form hash - {"add_key"=>"0key1234id1"}. Looks like post_form requires a string:string hash. How do I unpack the params hash back to the original parameter syntax - {"add_key[0] [key]" => "1234", add_key[0][id]" => "1"} ? I have used CGI.parse which works if the original request is a GET. But if the parameters are passed in a POST, it doesn''t work. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 14, 3:14 am, Vasu <vasukrish...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am doing an http get using the urlhttp://localhost/add?add_key[0][key]=1234&add_key[0][id]=1. > I have a rails app which gives me a neat params hash > {"add_key"=>{"0"=>{"key"=>"1234", "id"=>"1"}}. > > Now I try to post these to another server, using Net::HTTP.post_form > new_url,params > > The second server gets a form hash - {"add_key"=>"0key1234id1"}. > Looks like post_form requires a string:string hash. How do I unpack > the params hash back to the original parameter syntax - {"add_key[0] > [key]" => "1234", add_key[0][id]" => "1"} ? > > I have used CGI.parse which works if the original request is a GET. > But if the parameters are passed in a POST, it doesn''t work.Try the .to_query method 1.9.2p180 :004 > {"add_key"=>{"0"=>{"key"=>"1234", "id"=>"1"}}}.to_query => "add_key%5B0%5D%5Bid%5D=1&add_key%5B0%5D%5Bkey%5D=1234" (%5B and %5D are the encoded versions of [ and ]) Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
That works!! Thanks a lot, Fred. On Feb 14, 4:57 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 14, 3:14 am,Vasu<vasukrish...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I am doing an http get using the urlhttp://localhost/add?add_key[0][key]=1234&add_key[0][id]=1. > > I have a rails app which gives me a neat params hash > > {"add_key"=>{"0"=>{"key"=>"1234", "id"=>"1"}}. > > > Now I try to post these to another server, using Net::HTTP.post_form > > new_url,params > > > The second server gets a form hash - {"add_key"=>"0key1234id1"}. > > Looks like post_form requires a string:string hash. How do I unpack > > the params hash back to the original parameter syntax - {"add_key[0] > > [key]" => "1234", add_key[0][id]" => "1"} ? > > > I have used CGI.parse which works if the original request is a GET. > > But if the parameters are passed in a POST, it doesn''t work. > > Try the .to_query method > > 1.9.2p180 :004 > {"add_key"=>{"0"=>{"key"=>"1234", > "id"=>"1"}}}.to_query > => "add_key%5B0%5D%5Bid%5D=1&add_key%5B0%5D%5Bkey%5D=1234" > > (%5B and %5D are the encoded versions of [ and ]) > > Fred-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.