marcz2007

marcz2007

I am trying to post a test user account to the users table in the repo but I am getting the ‘no function clause matching’ error for some reason.

I have tried POSTing on both curl and Postman with the same results. Here is my post using CURL:
curl -H "Content-Type: application/json" -X POST -d '{"name":"test","id":"5"}' http://localhost:4000/api/users2

Another trial of CURL (in the style of the tutorial I am using - https://pamit.medium.com/building-a-restful-backend-with-elixir-phoenix-84fe390975c - uses):
curl -X POST http://localhost:4000/api/users2 -H “Content-Type: application/json” -d ‘{“accounts”: {“name”: “test”, “id”: “5"}}’

In Postman I am writing the URL with the JSON in the body:

http://localhost:4000/api/users2
{"name":"test","id":"5"}

Here is my create method which should be matching in order to add the new user account:

def create(conn, %{"user" => user_params}) do
    with {:ok, %User{} = user} <- Accounts.create_user(user_params) do
      conn
      |> put_status(:created)
      |> put_resp_header("location", Routes.user_path(conn, :show, user))
      |> render("show.json", user: user)
    end
  end

Showing Posts 1 to 5

axelson

axelson

Scenic Core Team

Your create function is not matching the parameters that you are sending via curl. A possible fix is to change the first line from:

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

to:

def create(conn, %{"accounts" => user_params}) do

Although personally i prefer to do little to no matching in controller function heads because I find the no function clause matching not very informative. If you changed your create to function to:

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

Then you would have gotten a more informative pattern matching error.

marcz2007

marcz2007 OP

Hi Axelson, many thanks for the reply! I am pretty new to Elixir and API calling so forgive me for my naivity but I have made the changes and am now getting an ‘undefined function’ error:

CompileError
lib/bank_api_web/controllers/user_controller.ex:26: undefined function user/0
Console output is shown below.
Compiling 1 file (.ex)

== Compilation error in file lib/bank_api_web/controllers/user_controller.ex ==
** (CompileError) lib/bank_api_web/controllers/user_controller.ex:26: undefined function user/0
    (elixir 1.11.2) src/elixir_locals.erl:114: anonymous fn/3 in :elixir_locals.ensure_no_undefined_local/3
    (stdlib 3.13.2) erl_eval.erl:680: :erl_eval.do_apply/6
    (elixir 1.11.2) lib/kernel/parallel_compiler.ex:314: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7

THis is now my create method:

 def create(conn, params) do
    %{"accounts" => user_params} = params
    |> put_status(:created)
    |> put_resp_header("location", Routes.user_path(conn, :show, user))
    |> render("show.json", user: user)
  end
kokolegorille

kokolegorille

It does not look like the code in the blog You are following… which looks like this

def create(conn, %{"business" => business_params}) do
  with {:ok, %Business{} = business} <- Directory.create_business(business_params) do
    conn
    |> put_status(:created)
    |> put_resp_header("location", business_path(conn, :show, business))
    |> render("show.json", business: business)
  end
end

Anyway, I would not recommend to follow this (old) post because things have changed since.

Maybe try this one, which cover Phoenix 1.5

https://lobotuerto.com/blog/building-a-json-api-in-elixir-with-phoenix/

BTW You are using user variable, which was never defined, and You define user_params, which is never used.

marcz2007

marcz2007 OP

Hi, thanks for your help. I have gone through the tutorial you suggested but find myself at the stage stage once more of trying to POST to my create_user route.

Here is my create function:

def create(conn, %{"user" => user_params}) do
    with {:ok, %User{} = user} <- Account.create_user(user_params) do
      conn
      |> put_status(:created)
      |> put_resp_header("location", Routes.user_path(conn, :show, user))
      |> render("show.json", user: user)
    end
  end

And this is the error in Postman:

no function clause matching in SpBankWeb.UserController.create/2
The following arguments were given to SpBankWeb.UserController.create/2:

    # 1
    %Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, assigns: %{}, before_send: [#Function<0.11227428/1 in Plug.Telemetry.call/2>], body_params: %{"amount" => "5.0", "name" => "test2"}, cookies: %{}, halted: false, host: "localhost", method: "POST", owner: #PID<0.471.0>, params: %{"amount" => "5.0", "name" => "test2"}, path_info: ["api", "users"], path_params: %{}, port: 4000, private: %{SpBankWeb.Router => {[], %{}}, :phoenix_action => :create, :phoenix_controller => SpBankWeb.UserController, :phoenix_endpoint => SpBankWeb.Endpoint, :phoenix_format => "json", :phoenix_layout => {SpBankWeb.LayoutView, :app}, :phoenix_request_logger => {"request_logger", "request_logger"}, :phoenix_router => SpBankWeb.Router, :phoenix_view => SpBankWeb.UserView, :plug_session_fetch => #Function<1.123471702/1 in Plug.Session.fetch_session/1>}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %{}, req_headers: [{"accept", "*/*"}, {"accept-encoding", "gzip, deflate, br"}, {"connection", "keep-alive"}, {"content-length", "31"}, {"content-type", "application/json"}, {"host", "localhost:4000"}, {"postman-token", "dba91f4a-d119-4ce9-a866-4be4fcc2a5b8"}, {"user-agent", "PostmanRuntime/7.26.8"}], request_path: "/api/users", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "FkraqL6-WjCGiuEAAACh"}], scheme: :http, script_name: [], secret_key_base: :..., state: :unset, status: nil}

    # 2
    %{"amount" => "5.0", "name" => "test2"}
marcz2007

marcz2007 OP

SOLVED: Inside the body of the HTML post on Postman, you need to include the exact variable name that the controller function uses (in my case this is ‘user’).

{“user”:{“name”:“test2”,“amount”:“5.0”}}

^^ This worked!

Thanks all who helped

— All posts loaded —

Where Next? Top

Trending in Questions Top

axelson
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
yasoob
Hi all! I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
neurodynamic
I have a page in my Phoenix app with some LiveView code. When you click a button, it shows the Stripe credit card modal form via a Hook. ...
New
Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
261 13183 112
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
charlenopires
Hi everyone :waving_hand: I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews