Parse Query Params From A URL
For all of the conveniences that Ruby and Rails affords a developer through their expansive APIs, I am always surprised that it is hard to inspect the query params in a URL.
Let's take a URL and walk through the steps it takes to pull out the value of a query param.
Here's a URL:
Let's parse the URL with URI
:
Then grab the query
part of that URI
:
This is an unparsed string. In a Rails context, this can be parsed with Rack::Utils.parse_nested_query
:
And now we have a hash of values we can inspect:
Be sure to do string and not symbol hash access here.
These steps can be wrapped up into a method:
Last updated