【发布时间】:2014-10-11 18:17:24
【问题描述】:
所以我试图在一个 URL 中传递多个参数来插入。这是我用于测试的 URL:
http://localhost:4000/uid=ToddFlanders&pwd=MyTestPword
这是代码:
defmodule Sci do
@userid "uid"
@password "pwd"
import Plug.Conn
import Plug.Conn.Utils
use Plug.Router
plug :match
plug :dispatch
get "/:args" do
%{@userid => usr} = params(args)
%{@password => pass} = params(args)
send_resp(conn, 200, "Hello #{usr}. Your password is #{pass}")
end
match _ do
send_resp(conn, 404, "oops")
end
def start do
Plug.Adapters.Cowboy.http Sci, [], port: 4000
end
def stop do
Plug.Adapters.Cowboy.shutdown Sci.HTTP
end
end
如果我只传递一个参数(uid 或 pwd),它匹配得很好。如果我删除查询字符串前面的?,它会失败——抛出异常。
我也试过这个:
get "/:args" do
%{@userid => usr, @password => pass} = params(args)
也没有用。所以两个问题:
1.) 这是在 URL 中传递多个参数的正确方法吗?
2.) 是否有一些技巧可以让插件同时查看两个参数?
欢迎任何想法或见解 - 甚至建议更好的方法来做到这一点。
Elixir v0.15.1 和 Plug v0.5.1
编辑:
根据 Jose 的评论添加堆栈跟踪:
iex(2)> 07:46:56.499 [error] Error in process <0.232.0> with exit value: {[{reason,{badmatch,#{}}},{mfa,{'Elixir.Plug.Adapters.Cowboy.Handler',init,3}},{stacktrace,[{'Elixir.Sci','-do_match/2-fun-0-',2,[{file,"lib/sci.ex"},{line,18}]},{'Eli
xir.Sci',call,2,[{file,"lib/sci.ex"},{line,1}]},{'Elixir.Plug.Adapters.Cowboy.Handler'...
iex(2)> 07:46:56.510 [error] Ranch listener Sci.HTTP had connection process started with :cowboy_protocol:start_link/4 at #PID<0.232.0> exit with reason: {[reason: {:badmatch, %{}}, mfa: {Plug.Adapters.Cowboy.Handler, :init, 3}, stacktrace:
[{Sci, :"-do_match/2-fun-0-", 2, [file: 'lib/sci.ex', line: 18]}, {Sci, :call, 2, [file: 'lib/sci.ex', line: 1]}, {Plug.Adapters.Cowboy.Handler, :init, 3, [file: 'lib/plug/adapters/cowboy/handler.ex', line: 7]}, {:cowboy_handler, :handler_
init, 4, [file: 'src/cowboy_handler.erl', line: 64]}, {:cowboy_protocol, :execute, 4, [file: 'src/cowboy_protocol.erl', line: 435]}], req: [socket: #Port<0.5168>, transport: :ranch_tcp, connection: :keepalive, pid: #PID<0.232.0>, method: "G
ET", version: :"HTTP/1.1", peer: {{127, 0, 0, 1}, 63880}, host: "localhost", host_info: :undefined, port: 4000, path: "/uid=ToddFlanders&pwd=MyTestPassword", path_info: :undefined, qs: "", qs_vals: :undefined, bindings: [], headers: [{"host
", "localhost:4000"}, {"connection", "keep-alive"}, {"cache-control", "max-age=0"}, {"accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"}, {"user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/53
7.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36"}, {"accept-encoding", "gzip,deflate,sdch"}, {"accept-language", "en-US,en;q=0.8"}], p_headers: [{"connection", ["keep-alive"]}], cookies: :undefined, meta: [], body_state: :waitin
g, buffer: "", multipart: :undefined, resp_compress: false, resp_state: :waiting, resp_headers: [], resp_body: "", onresponse: :undefined], opts: {Sci, []}], [{:cowboy_protocol, :execute, 4, [file: 'src/cowboy_protocol.erl', line: 435]}]}
iex(2)> 07:46:56.969 [error] Error in process <0.233.0> with exit value: {[{reason,{badmatch,#{}}},{mfa,{'Elixir.Plug.Adapters.Cowboy.Handler',init,3}},{stacktrace,[{'Elixir.Sci','-do_match/2-fun-0-',2,[{file,"lib/sci.ex"},{line,18}]},{'Eli
xir.Sci',call,2,[{file,"lib/sci.ex"},{line,1}]},{'Elixir.Plug.Adapters.Cowboy.Handler'...
iex(2)> 07:46:56.971 [error] Ranch listener Sci.HTTP had connection process started with :cowboy_protocol:start_link/4 at #PID<0.233.0> exit with reason: {[reason: {:badmatch, %{}}, mfa: {Plug.Adapters.Cowboy.Handler, :init, 3}, stacktrace:
[{Sci, :"-do_match/2-fun-0-", 2, [file: 'lib/sci.ex', line: 18]}, {Sci, :call, 2, [file: 'lib/sci.ex', line: 1]}, {Plug.Adapters.Cowboy.Handler, :init, 3, [file: 'lib/plug/adapters/cowboy/handler.ex', line: 7]}, {:cowboy_handler, :handler_
init, 4, [file: 'src/cowboy_handler.erl', line: 64]}, {:cowboy_protocol, :execute, 4, [file: 'src/cowboy_protocol.erl', line: 435]}], req: [socket: #Port<0.5174>, transport: :ranch_tcp, connection: :keepalive, pid: #PID<0.233.0>, method: "G
ET", version: :"HTTP/1.1", peer: {{127, 0, 0, 1}, 63881}, host: "localhost", host_info: :undefined, port: 4000, path: "/favicon.ico", path_info: :undefined, qs: "", qs_vals: :undefined, bindings: [], headers: [{"host", "localhost:4000"}, {"
connection", "keep-alive"}, {"accept", "*/*"}, {"user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36"}, {"accept-encoding", "gzip,deflate,sdch"}, {"accept-language", "e
n-US,en;q=0.8"}], p_headers: [{"connection", ["keep-alive"]}], cookies: :undefined, meta: [], body_state: :waiting, buffer: "", multipart: :undefined, resp_compress: false, resp_state: :waiting, resp_headers: [], resp_body: "", onresponse:
:undefined], opts: {Sci, []}], [{:cowboy_protocol, :execute, 4, [file: 'src/cowboy_protocol.erl', line: 435]}]}
编辑 2:
我在上面记下了错误的 URL;现在已经纠正了。还要补充一点,如果我将 URL 设为:http://localhost:4000/?uid=ToddFlanders&pwd=MyTestPword,则代码会跳转到match _ do 子句
【问题讨论】:
-
你遇到了什么失败?你能发布堆栈跟踪吗?而且你肯定需要在之前调用 fetch_params。
-
我已经添加了堆栈跟踪。我之前犹豫要不要添加它,因为我不太清楚如何格式化它。
-
啊,我明白了。
:args不会包含查询字符串。它将只包含路径。如果要访问查询字符串参数,则需要完全按照 sasa 建议的方式进行操作。这基本上是调用fetch_params(conn)并以conn.params访问它。
标签: elixir