I’m making a post request using the built-in net/http
ruby library, and I need to update it to post nested parameters.
Currently, I get (from another object) the params in the following format:
{ :user => {:code=>"00004812", :valid_from=>"2024-01-01"} }
But If I try to post them as is, the endpoint receives them as a string:
Parameters: {"user"=>"{"code"=>"00004812", "valid_from"=>"2024-01-01"}"
And they are not correctly parsed.
I tried to change them manually as:
{"user[code]" => "00004812", "user[valid_from]" => "2024-01-01"}
And, with this format, they are received correctly:
Parameters: {"user"=>{"code"=>"00004812", "valid_from"=>"2024-01-01"}}
Is there a way to make the change programmatically without changing how the data is received from the other object?
My code looks like:
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(params)
response = http.request(request)
1