Controller pattern matching post body params, => vs short-hand

Somewhat a newbie question but consider the following route where the expecting behavior is the post body would include user_id and password:

post "/api/login", LoginController, :login

It seems that all the Phoenix examples pattern match this use the explicit => syntax and not the short-hand. I am wondering is there a reason for one approach vs the other?

Example:

# Option 1
def login(conn, %{"user_id" => user_id, "password" => password} = _params) do

# Option 2
def login(conn, %{user_id: user_id, password: password} = _params) do ....
1 Like

Only the first (string keyed) pattern matching is the correct way. The second one is atom based ans controllers pass string as param keys.

2 Likes

Sorry I re-read the question and you wanted the reasoning.

So body parameters are external daya and by converting external data into atoms we would risk filling up the VMs limit. So the convention is to use string as keys. I think the Programming Phoenix book explains this nicely.

This SO has an explanation to this:

Update Reason: Added the SO link.

3 Likes

Ahhh perfect, I had read / was aware of the atom limit but failed to make the connection that the body_params are user submitted / generated so Phoenix would pass them to controllers as string based keys.

Thank you!

2 Likes