Today I Learned #4: Passing array/list parameters to Phoenix controller

Hello!

I had curious incident while working. I was to investigate why our API won’t work when passed array as url parameter for GET request.

So, I peeked into the tests and we had test for it. I logged the incoming parameters and Phoenix got an array and worked with it perfectly. So I decided to grab the big guns. Enter curl.

It was quite funny. My first insting was to create url like this:

some.url?param=[value1,value2]

but it didn’t work. Phoenix read this as

%{"param" => "[value1,value2]"}

and no combination of quotes would make this work.

To skip some other things I tried - the thing you need to do is to use this

some_url?param[]=value1&param[]=value2

Phoenix will read all those values as elements of an array (or, in Elixir’s context, a list) and it will work as intended.

Written on April 10, 2020