# 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:

```ruby
url = "https://example.com?taco=bell&taco_count=3"
=> "https://example.com?taco=bell&taco_count=3"
```

Let's parse the URL with `URI`:

```ruby
> URI(url)
=> #<URI::HTTPS https://example.com?taco=bell&taco_count=3>
```

Then grab the `query` part of that `URI`:

```ruby
> URI(url).query
=> "taco=bell&taco_count=3"
```

This is an unparsed string. In a Rails context, this can be parsed with `Rack::Utils.parse_nested_query`:

```ruby
> query_params = Rack::Utils.parse_nested_query(URI(url).query)
=> {"taco"=>"bell", "taco_count"=>"3"}
```

And now we have a hash of values we can inspect:

```ruby
> query_params["taco_count"]
=> "3"
```

Be sure to do *string* and not *symbol* hash access here.

These steps can be wrapped up into a method:

```ruby
module UrlHelpers
  def query_params(url)
    unparsed_query_params = URI(url).query
    Rack::Utils.parse_nested_query(unparsed_query_params)
  end
end
```

[source](https://stackoverflow.com/a/3218018/535590)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/rails/parse-query-params-from-a-url.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
