Skip to content

Instantly share code, notes, and snippets.

@qsona
Created August 18, 2017 06:24
Show Gist options
  • Save qsona/6ea7b719d41433e668ce238cddfed409 to your computer and use it in GitHub Desktop.
Save qsona/6ea7b719d41433e668ce238cddfed409 to your computer and use it in GitHub Desktop.
form encode/decode
Faraday::NestedParamsEncoder.encode({"a"=>[{"b"=>"1", "c"=>"2"}, {"b"=>"3", "c"=>"4"}]})
# => "a%5B%5D%5Bb%5D=1&a%5B%5D%5Bc%5D=2&a%5B%5D%5Bb%5D=3&a%5B%5D%5Bc%5D=4"
URI.decode('a%5B%5D%5Bb%5D=1&a%5B%5D%5Bc%5D=2&a%5B%5D%5Bb%5D=3&a%5B%5D%5Bc%5D=4')
# => "a[][b]=1&a[][c]=2&a[][b]=3&a[][c]=4"
Faraday::NestedParamsEncoder.encode({"a"=>[{"b"=>"1", "c"=>"2"}, {"d"=>"3", "e"=>"4"}]})
# => "a%5B%5D%5Bb%5D=1&a%5B%5D%5Bc%5D=2&a%5B%5D%5Bd%5D=3&a%5B%5D%5Be%5D=4"
URI.decode("a%5B%5D%5Bb%5D=1&a%5B%5D%5Bc%5D=2&a%5B%5D%5Bd%5D=3&a%5B%5D%5Be%5D=4")
# => "a[][b]=1&a[][c]=2&a[][d]=3&a[][e]=4"
Faraday::NestedParamsEncoder.decode("a%5B%5D%5Bb%5D=1&a%5B%5D%5Bc%5D=2&a%5B%5D%5Bd%5D=3&a%5B%5D%5Be%5D=4")
# => {"a"=>[{"b"=>"1", "c"=>"2", "d"=>"3", "e"=>"4"}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment