0
votes

I'm trying to create a project but I'm unable to create objects via the API, I'm using only commands to create the Phoenix project and it still doesn't work.

Steps to reproduce:

I'm using the following commands

mix phx.new project --app project --module TodoList --no-html --no-webpack
cd project
mix ecto.create
mix phx.gen.json Accounts User users first_name:string last_name:string

Then I add the resource in my router.ex file like so:

  scope "/api", TodoListWeb do
    pipe_through :api

    resources "/users", UserController
  end

And finnally:

mix ecto.migrate
mix phx.server

After that the server start without any issues, up to this point everything is fine, but when I do a POST request on http://localhost:4000/api/users using Postman, with any parameters I get a 400 status code.

With the following message:

enter image description here

I can see in the console the intput of the request:

%{"first_name" => "Jeff", "last_name" => "Doe"}
1

1 Answers

0
votes

I think you need to specify an outermost "user" key in the request, as in:

%{"user" => %{"first_name" => "Jeff", "last_name" => "Doe"}}

So your JSON request on Postman needs to be:

{
    "user": {
        "first_name": "Jeff",
        "last_name": "Doe"
    }
}