2
votes

I have defined the following routes in router.ex

    scope "/api/v1", ProjWeb do
        pipe_through :api

        post "/sign_up", UserController, :sign_up
        post "/sign_in", UserController, :sign_in

      end

Here is the controller actions

def sign_up(conn, %{"user" => user_params}) do
 # example code
 # response
end

def sign_in(conn, %{"user" => %{"email" => email, "password" => password}}) do  
 ### resp
end

/api/v1/sign_up works with the following payload

{
    "user": {
        "email": "[email protected]",
        "name": "Krishna",
        "password": "SUPER_SECRET_PASS!"
    }
}
/api/v1/sign_in

with the following payload

{
    "user": {
        "email": "[email protected]",
        "password": "SUPER_SECRET_PASS!"
    }
}

Throws the following error

no function clause matching in ProjWeb.UserController.sign_in/2

Logs:

 # 1
   %Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, assigns: %{}, before_send:...}
   # 2
   %{}
2
How are you sending this? Do you have the proper header to let Phoenix know to expect json? Content-Type: application/jsonJustin Wood
@JustinWood thanks ! , I was using postman to create the request and instead of setting Content-Type: application/json I was sending Content-Type: application/javascript . would be happy to accept an answer if you post it.Vamsi Krishna B

2 Answers

2
votes

Your payload doesn't seem to come through correctly, since the second argument you pass is an empty map (see logs). Since your sign_in function expects a struct with a user key, it cannot match your call.

1
votes

It sounds like you have the wrong Content-Type header set. You will need to make sure you are sending Content-Type: application/json in order for Phoenix to know that you are sending JSON information.