Only the first querystring parameters is in the params of the controller?


I’ve a fairly fresh Phoenix API only project, and I’m not able to figure out why a controler action would only have the first querystring parameter in the params argument.

Calling this with curl: curl ... http://localhost:4000/api/ctrlhere?page=1&status=open

Getting this in the log:

[debug] Processing with XYZWeb.CtrlHereController.index/2
  Parameters: %{"status" => "open"}
  Pipelines: [:api, :api_auth]

At first I had this as the function prototype:

def index(conn, %{"status" => status, "page" => page})

And was getting a 400 bad request error.

Changing the order of the querystring param in my URL, for instance ?page=1&status=open

Now the logger prints the %{"page" => "1"}

How can we pass multiple querystring parameters to controller via URL?


You might consider case without parameters… it will break your pattern match.

You can do this instead.

def index(conn, params) do
  page = Map.get(params, "page", 1)
  # ...

I understand your point of “what happened if no QS is passed”

BUT, nonetheless, my issue is that this params is only receiving the first querystring parameter for now, and that’s what I’m trying to solve:

for instance:

def index(conn, params) do

Calling with page=1&status=open I would expect the output to print:

%{"page" => "1", "status" => "open")}

Yet I’m only receiving the first parameter of the querystring

1 Like

Might be that you have a plug that’s removing that param form the list, IDK

1 Like

What is the result of…

IO.inspect(params, label: "params")
[debug] Processing with ParleWeb.ConversationController.index/2
  Parameters: %{"page" => "1"}
  Pipelines: [:api, :api_auth]
params: %{"page" => "1"}

My exact CURL line:

curl -v -H "Accept: application/json" -H "Authorization: Bearer token-here" http://localhost:4000/api/conversations?page=1&status=open

It’s a huge non-sense, but that’s what I’m getting. And inverting the order of the QS will prints the first one in the list, so status=open&page=1 will print status only

ho the & breaks the curl command, I’m sorry about this. It was the URL needed to be double quoted :wink: