Bleupi
Hello,
I’m struggling to figure out why my api is sending me a 400 error on create route. Show and index route are working fine.
I tried different playload, I double check there was no error in json playload and
Here is my controller (auto generated with phoenix generator):
defmodule ColdDataApiWeb.ProjectController do
use ColdDataApiWeb, :controller
alias ColdDataApi.Record
alias ColdDataApi.Record.Project
action_fallback ColdDataApiWeb.FallbackController
def index(conn, _params) do
projects = Record.list_projects()
render(conn, "index.json", projects: projects)
end
def create(conn, %{"project" => project_params}) do
with {:ok, %Project{} = project} <- Record.create_project(project_params) do
conn
|> put_status(:created)
|> put_resp_header("location", project_path(conn, :show, project))
|> render("show.json", project: project)
end
end
def show(conn, %{"id" => id}) do
project = Record.get_project!(id)
render(conn, "show.json", project: project)
end
def update(conn, %{"id" => id, "project" => project_params}) do
project = Record.get_project!(id)
with {:ok, %Project{} = project} <- Record.update_project(project, project_params) do
render(conn, "show.json", project: project)
end
end
def delete(conn, %{"id" => id}) do
project = Record.get_project!(id)
with {:ok, %Project{}} <- Record.delete_project(project) do
send_resp(conn, :no_content, "")
end
end
end
My schema:
defmodule ColdDataApi.Record.Project do
use Ecto.Schema
import Ecto.Changeset
schema "projects" do
field :name, :string
field :short_description, :string
field :description, :string
field :human_name, :string
end
@allowed_fields [:name, :short_description, :description, :human_name]
@required_fields [:name, :short_description, :human_name]
@doc false
def changeset(project, attrs) do
project
|> cast(attrs, @allowed_fields)
|> validate_required(@required_fields)
|> validate_length(:name, max: 40, count: :codepoints)
|> validate_length(:short_description, max: 600, count: :codepoints)
|> validate_length(:human_name, max: 40, count: :codepoints)
|> validate_length(:description, max: 65_535, count: :codepoints)
|> unique_constraint(:name)
end
end
My FallbackController:
defmodule ColdDataApiWeb.FallbackController do
@moduledoc """
Translates controller action results into valid `Plug.Conn` responses.
See `Phoenix.Controller.action_fallback/1` for more details.
"""
use ColdDataApiWeb, :controller
def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
conn
|> put_status(:unprocessable_entity)
|> render(ColdDataApiWeb.ChangesetView, "error.json", changeset: changeset)
end
def call(conn, {:error, :not_found}) do
conn
|> put_status(:not_found)
|> render(ColdDataApiWeb.ErrorView, :"404")
end
end
I add the routes like this:
# Other scopes may use custom stacks.
scope "/api/v1", ColdDataApiWeb do
pipe_through :api
resources "/projects", ProjectController, except: [:new, :edit]
end
I tested in iex console, Record.create_project works. I suppose the error come from the controller.
Does anyone could give me an hint on this error ?
Ps: english is not my mother tongue, so do not hesitate to ask me question if something is unclear.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
Can you provide the output of
mix phx.routes?And how do you send the request?
idi527
Try putting a few
IO.inspect/1calls into thecreateaction:and making the request again. These inspect calls might help reveal the problem.
Bleupi
The output of
mix phx.routesis:To send the request is tried both postman and curl with the same payload:
About putting the IO.inspect, I did it but I’m running the api into a docker container, and I don’t get outputs yet for this command (I get other logs through). So, once I figure out how to get thos output I’ll show you the results
NobbZ
There is no
"project"key in your body. Thats the first thing I do see with your snippet.Perhaps try a catch-all header for now (
def create(conn, params) do...) and as the very first line doLogger..debug(params).Bleupi
So I added the
"project"key in the request body:And With the logger I get the following error:
NobbZ
Now you used a
"project"key in the body and you obviously didn’t use a catch all clause. Just remove the log-call and you should be good to go.Bleupi
Thanks a lot ! It’s working.