That is pretty easy with a plain hash like
{:a => "a", :b => "b"}
which would translate into
"a=a&b=b"
But what do you do with something more complex like
{:a => "a", :b => ["c", "d", "e"]}
which should translate into
"a=a&b[0]=c&b[1]=d&b[2]=e"
Or even worse, (what to do) with something like:
{:a => "a", :b => [{:c => "c", :d => "d"}, {:e => "e", :f => "f"}]
Thanks for the much appreciated help with that!
Update: This functionality was removed from the gem.
Julien, your self-answer is a good one, and I've shameless borrowed from it, but it doesn't properly escape reserved characters, and there are a few other edge cases where it breaks down.
The gem is 'addressable'
Here's another way. For simple queries.
I like using this gem:
https://rubygems.org/gems/php_http_build_query
Sample usage:
No need to load up the bloated ActiveSupport or roll your own, you can use
Rack::Utils.build_query
andRack::Utils.build_nested_query
. Here's a blog post that gives a good example:It even handles arrays:
Or the more difficult nested stuff:
Here's a short and sweet one liner if you only need to support simple ASCII key/value query strings: