How to get current request url full query params (as a keyword list if possible) inside a phoenix template?

For example for a current url like
http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2,
I would like to get:

[_utf8: “✓”, query: “perfume”, page: “2”]

I tought I could use @conn.assigns.params but I realized it may contain other stuff that I don’t need here. I just want only the params present in the url after the ?.

I think this will do what you want

https://hexdocs.pm/plug/Plug.Conn.html#fetch_query_params/2

1 Like

This triggers parsing them if they haven’t been parsed yet.

To actually get the params from the query string, one could just do conn.query_params IIRC. They will return as a map though, not as a keywordlist (which would be dangerous anyway as it would open your system for atom overflow attacks)

2 Likes

Thanks for the warning. When converting the Map to a Keyword List, I will then match only the allowed query param keys. I think this will avoid atom overflow attacks.

Ah you’re right!

2 Likes