« Back to Index

Ruby parsing of complex nested data structures from the Query String

View original Gist on GitHub

ruby-query-string-nested-data-structure.rb

require "rack"
require "cgi"
require "addressable/uri"
require "pry"

class HelloWorldApp
  def self.call(env)
    p CGI.parse(env["QUERY_STRING"]) # {"a"=>["a"], "b[c]"=>["c"], "b[d]"=>["d"]}

    uri = Addressable::URI.parse(env["REQUEST_URI"])
    p uri.query_values # {"a"=>"a", "b[c]"=>"c", "b[d]"=>"d"}

    binding.pry
    [200, {}, "Hello World"]
  end
end

Rack::Server.start :app => HelloWorldApp

# http://localhost:8080/?a=a&b[0][c]=c&b[0][d]=d&b[1][e]=e&b[1][f]=f