POST to generated json api not working (no matching action)

Hi folks!

I have created a new project with:

mix phoenix.new dummy --no-html --no-brunch

And then generated a json api with:

mix phoenix.gen.json User users name:string age:integer

Now, when i want to add a user via Postman:

…Phoenix will throw the following at me:

bad request to Dummy.UserController.create, no matching action clause to process request

Am i not using this api correctly? I would assume that this is the correct way to do it. For this to work it would be neccessary that the json body is automatically parsed and added to the request parameters. I know that it is possible to use Plug.Conn.read_body to get the response body directly from the connection, but then i would expect the generated controller to use this function.

I am using Elixir 1.4 and Phoenix 1.2.1 on Win 10.

Thanks in advance!

2 Likes

Correct me if I’m wrong, but I guess that given your parameters, Phoenix generates the create action with the following signature:

def create(conn, %{"user" => user_params}) do
  ...
end

So the POST body should be:

{
  "user" => {
    "name" => "sloschi",
    "age": 32
  }
}
2 Likes

You are absolutely correct, if i use the following json structure it works:

{
"user": {
	"name": "sloschi",
	"age": 32
}
}

I was pretty sure i did try that, but i obviously must have messed up the json syntax or something.

Well, that was easy…thanks!

3 Likes