0
votes

I am using Phoenix as a basic JSON api but I'm having some issues working out how to check that the requester has passed in the correct arguments.

Say the user makes a post request that requires the following body:

{
  "user": {
    "email": "[email protected]",
    "password": "mangoes"
  }
}

I want to check that the user has passed in an email and password in the user object. At the moment I'm doing something like this in my controller actions:

params[user][email] and params[user][password]

Surely there is a better way to check for the correct parameters. Maybe something to do with scrub_params? If so how do I return a nice JSON error message instead of the default Internal server error?

Any advice is appreciated.

1

1 Answers

3
votes

You can check it in the parameter in the action itself

  def show(conn, %{"user" => %{"email" => email, "password" => password}} = params) do
    render conn, "show.html", messenger: messenger
  end

For an example on this approach, head to the offical docs here