KavitaC KavitaC - 1 year ago 203
HTTP Question

Ruby: http POST with nested params not working

I am trying to do Http Basic Authentication with POST and nested parameters.
While the outer parameters work fine (class.name - ActionController::Parameters) the nested parameters are string (class.name - String)
Here is my code ->

require 'net/http'

uri = URI('http://example.com/bulb/')
req = Net::HTTP::Post.new(uri)
req.basic_auth 'mytest@somesite.com', 'mypassword'

req.set_form_data('first_params' => 'a', 'seconnd_params'=>'b', 'netsed_params'=>{'first_netsed'=>'c', 'second_nested'=>'d'}, 'commit'=>'Create Bulb', 'action'=>'create', 'controller'=>'bulb')

res = Net::HTTP.start(uri.hostname, uri.port) do |http|

case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK

What other library can I use to make the nested params work without having to manually convert them. I see that
doesn't work with nested hash

Answer Source

Try this

req.set_form_data('first_params' => 'a', 'second_params'=>'b', 'nested_params[first_nested]' => 'c', 'nested_params[second_nested]' => 'd'}, 'commit'=>'Create Bulb', 'action'=>'create', 'controller'=>'bulb')