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.
Marked As Solved
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.
Popular in Questions
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
I am trying to implement my new.html.eex file to create new posts on my website.
new.html.eex:
<h1>Create Post</h1>
<%= ...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> somethi...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Other popular topics
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








