Bleupi
Simple Api where create function send me 400 "Bad Request" but I can't figure out why
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 in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Hello !
We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










First 7 of 7 Posts
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.