user2602079
user2602079

Reputation: 1393

Elixir - Plug - no function clause matching

I'm sending this PUT http request: http://192.168.20.7:4000/products/?id=76

I'm getting this error:

18:31:31.575 [error] #PID<0.505.0> running Api.Router terminated
Server: 192.168.20.7:4000 (http)
Request: PUT /products/?id=76
** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in Api.Router.do_match/4
        (api) lib/api/router.ex:26: Api.Router.do_match(%Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :..
.}, assigns: %{}, before_send: [], body_params: %Plug.Conn.Unfetched{aspect: :body_params}, cookies: %Plug.
Conn.Unfetched{aspect: :cookies}, halted: false, host: "192.168.20.7", method: "PUT", owner: #PID<0.505.0>,
 params: %Plug.Conn.Unfetched{aspect: :params}, path_info: ["products"], path_params: %{}, peer: {{192, 168
, 20, 16}, 59728}, port: 4000, private: %{}, query_params: %Plug.Conn.Unfetched{aspect: :query_params}, que
ry_string: "id=76", remote_ip: {192, 168, 20, 16}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req
_headers: [{"host", "192.168.20.7:4000"}, {"content-type", "application/json"}, {"user-agent", "vepo/1 CFNe
twork/811.5.4 Darwin/16.6.0"}, {"connection", "keep-alive"}, {"accept", "*/*"}, {"accept-language", "en-us"
}, {"content-length", "66"}, {"accept-encoding", "gzip, deflate"}, {"x-requested-with", "XMLHttpRequest"}],
 request_path: "/products/", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0
, private, must-revalidate"}], scheme: :http, script_name: [], secret_key_base: nil, state: :unset, status:
 nil}, "PUT", ["products"], "192.168.20.7")
        (api) lib/api/router.ex:1: Api.Router.plug_builder_call/2
        (api) lib/plug/debugger.ex:123: Api.Router.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) /Users/Ben/Development/Projects/vepo/api/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_
protocol.execute/4

I have this in my router to handle the request:

  put "/products/:id" do
    IO.puts("putting image #{id}")
    conn
      |> put_resp_content_type("application/json")
      |> send_resp(200, Poison.encode!(%{
          successs: "success"
      }))
  end

I'm aware the error means my router is not handling the request. Shouldn't this path put "/products/:id" manage that request?

Upvotes: 0

Views: 1952

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 120990

For the sake of future readers:

put "/products/:id"

is mapped to:

#                                ⇓⇓⇓
http://192.168.20.7:4000/products/76

not to:

#                                 ⇓⇓⇓⇓⇓⇓ 
http://192.168.20.7:4000/products/?id=76

The latter is mapped to

put "/products"

Upvotes: 3

Related Questions