Elixir - Plug - 无功能子句匹配

时间:2017-06-27 06:38:31

标签: elixir elixir-framework

我正在发送此E 000 000 0001 E 000 000 0002 X 000 000 0003 X 000 000 0004 X 000 000 0005 http请求:http://192.168.20.7:4000/products/?id=76

我收到了这个错误:

PUT

我在路由器中有这个来处理请求:

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

我知道错误意味着我的路由器没有处理请求。此路径 put "/products/:id" do IO.puts("putting image #{id}") conn |> put_resp_content_type("application/json") |> send_resp(200, Poison.encode!(%{ successs: "success" })) end 不应该管理该请求吗?

1 个答案:

答案 0 :(得分:3)

为了未来的读者:

put "/products/:id"

映射到:

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

不要:

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

后者映射到

put "/products"