I’ve run into a problem making a simple API leveraging mix phx.gen.json.
Steps to reproduce:
- Make a new phoenix application (mix phx.new sd)
- cd sd
- mix ecto.create
- mix phx.gen.json People Prospect prospects email:string:unique slot_score:integer sms_phone:string
- mix ecto.migrate
- uncomment API scope in router.ex
- add resources “/prospect”, ProspectController inside the “/api” scope block
- mix test (17 tests pass, 0 failures)
- mix phx.server (running on localhost:4000)
- using a http client (Postman) send
POST http://localhost:4000/api/prospect?email=test@test.com&slot_score:=1&sms_phone=8015551212
The result is the following message in the console:
[info] POST /api/prospect
[debug] Processing with SdWeb.ProspectController.create/2
Parameters: %{“email” => “test@test.com”, “slot_score:” => “1”, “sms_phone” => “8015551212”}
Pipelines: [:api]
[info] Sent 400 in 45ms
[debug] ** (Phoenix.ActionClauseError) no function clause matching in SdWeb.ProspectController.create/2The following arguments were given to SdWeb.ProspectController.create/2:
# 1 %Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, assigns: %{}, before_send: [#Function<1.112466771/1 in Plug.Logger.call/2>, #Function<0.33752287/1 in Phoenix.LiveReloader.before_send_inject_reloader/2>], body_params: %{}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "localhost", method: "POST", owner: #PID<0.575.0>, params: %{"email" => "test@test.com", "slot_score:" => "1", "sms_phone" => "8015551212"}, path_info: ["api", "prospect"], path_params: %{}, port: 4000, private: %{SdWeb.Router => {[], %{}}, :phoenix_action => :create, :phoenix_controller => SdWeb.ProspectController, :phoenix_endpoint => SdWeb.Endpoint, :phoenix_format => "json", :phoenix_layout => {SdWeb.LayoutView, :app}, :phoenix_pipelines => [:api], :phoenix_router => SdWeb.Router, :phoenix_view => SdWeb.ProspectView, :plug_session_fetch => #Function<1.58261320/1 in Plug.Session.fetch_session/1>}, query_params: %{"email" => "test@test.com", "slot_score:" => "1", "sms_phone" => "8015551212"}, query_string: "email=test@test.com&slot_score:=1&sms_phone=8015551212", remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{"accept", "*/*"}, {"accept-encoding", "gzip, deflate"}, {"cache-control", "no-cache"}, {"connection", "keep-alive"}, {"content-length", "0"}, {"cookie", "_scandimension_key=SFMyNTY.g3QAAAABbQAAAA9jdXJyZW50X3VzZXJfaWRhAQ.HORxU__-f-buE_aqcmnL1jb4AGUJ7qUL7ZJI-9JEUFY"}, {"host", "localhost:4000"}, {"postman-token", "f2ccbbee-93e7-4d2e-9fb5-2c0dbf6a3c60"}, {"user-agent", "PostmanRuntime/7.6.1"}], request_path: "/api/prospect", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "FZbzBCQZewyGPQoAAARD"}], scheme: :http, script_name: [], secret_key_base: :..., state: :unset, status: nil} # 2 %{"email" => "test@test.com", "slot_score:" => "1", "sms_phone" => "8015551212"} (sd) lib/sd_web/controllers/prospect_controller.ex:14: SdWeb.ProspectController.create(%Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, assigns: %{}, before_send: [#Function<1.112466771/1 in Plug.Logger.call/2>, #Function<0.33752287/1 in Phoenix.LiveReloader.before_send_inject_reloader/2>], body_params: %{}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "localhost", method: "POST", owner: #PID<0.575.0>, params: %{"email" => "test@test.com", "slot_score:" => "1", "sms_phone" => "8015551212"}, path_info: ["api", "prospect"], path_params: %{}, port: 4000, private: %{SdWeb.Router => {[], %{}}, :phoenix_action => :create, :phoenix_controller => SdWeb.ProspectController, :phoenix_endpoint => SdWeb.Endpoint, :phoenix_format => "json", :phoenix_layout => {SdWeb.LayoutView, :app}, :phoenix_pipelines => [:api], :phoenix_router => SdWeb.Router, :phoenix_view => SdWeb.ProspectView, :plug_session_fetch => #Function<1.58261320/1 in Plug.Session.fetch_session/1>}, query_params: %{"email" => "test@test.com", "slot_score:" => "1", "sms_phone" => "8015551212"}, query_string: "email=test@test.com&slot_score:=1&sms_phone=8015551212", remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{"accept", "*/*"}, {"accept-encoding", "gzip, deflate"}, {"cache-control", "no-cache"}, {"connection", "keep-alive"}, {"content-length", "0"}, {"cookie", "_scandimension_key=SFMyNTY.g3QAAAABbQAAAA9jdXJyZW50X3VzZXJfaWRhAQ.HORxU__-f-buE_aqcmnL1jb4AGUJ7qUL7ZJI-9JEUFY"}, {"host", "localhost:4000"}, {"postman-token", "f2ccbbee-93e7-4d2e-9fb5-2c0dbf6a3c60"}, {"user-agent", "PostmanRuntime/7.6.1"}], request_path: "/api/prospect", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "FZbzBCQZewyGPQoAAARD"}], scheme: :http, script_name: [], secret_key_base: :..., state: :unset, status: nil}, %{"email" => "test@test.com", "slot_score:" => "1", "sms_phone" => "8015551212"}) (sd) lib/sd_web/controllers/prospect_controller.ex:1: SdWeb.ProspectController.action/2 (sd) lib/sd_web/controllers/prospect_controller.ex:1: SdWeb.ProspectController.phoenix_controller_pipeline/2 (sd) lib/sd_web/endpoint.ex:1: SdWeb.Endpoint.instrument/4 (phoenix) lib/phoenix/router.ex:275: Phoenix.Router.__call__/1 (sd) lib/sd_web/endpoint.ex:1: SdWeb.Endpoint.plug_builder_call/2 (sd) lib/plug/debugger.ex:122: SdWeb.Endpoint."call (overridable 3)"/2 (sd) lib/sd_web/endpoint.ex:1: SdWeb.Endpoint.call/2 (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:33: Phoenix.Endpoint.Cowboy2Handler.init/2 (cowboy) /Users/chipcoons/Dev/Phoenix/sd/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2 (cowboy) /Users/chipcoons/Dev/Phoenix/sd/deps/cowboy/src/cowboy_stream_h.erl:296: :cowboy_stream_h.execute/3 (cowboy) /Users/chipcoons/Dev/Phoenix/sd/deps/cowboy/src/cowboy_stream_h.erl:274: :cowboy_stream_h.request_process/3 (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
I would think that the code from phx.gen.json has been used by a lot of the community, so am assuming I missed something simple and obvious, but am pulling my hair out trying to figure out what it is.
Any thoughts?


















