根本无法向Erlang / Cowboy提出请求。我可以从erlang shell中创建一个,但在运行牛仔版本时却不能。尝试使用'hackney'库:
hackney:start(),
{ok, _, _, Ref} = hackney:request(
get, <<"http://www.youtube.com">>, [], <<>>, [{pool, default}]
),
{ok, Body} = hackney:body(Ref),
io:format("body: ~p~n~n", [Body]),
错误:
Error in process <0.361.0> on node 'cta_erlang_backend@127.0.0.1' with exit value:
{[{reason,undef},
{mfa,{hello_handler,handle,2}},
{stacktrace,[{hackney,start,[],[]},
{hello_handler,handle,2,
[{file,"src/hello_handler.erl"},{line,18}]},
{cowboy_handler,handler_handle,4,
[{file,"src/cowboy_handler.erl"},{line,111}]},
{cowboy_protocol,execute,4,
[{file,"src/cowboy_protocol.erl"},
{line,442}]}]},
{req,[{socket,#Port<0.267>},
{transport,ranch_tcp},
{connection,keepalive},
{pid,<0.361.0>},
{method,<<"POST">>},
{version,'HTTP/1.1'},
{peer,{{10,0,0,1},40049}},
{host,<<"10.0.0.103">>},
{host_info,undefined},
{port,8080},
{path,<<"/">>},
{path_info,undefined},
{qs,<<>>},
{qs_vals,undefined},
{bindings,[]},
{headers,[{<<"host">>,<<"10.0.0.103:8080">>},
{<<"connection">>,<<"keep-alive">>},
{<<"content-length">>,<<"4">>},
{<<"cache-control">>,<<"no-cache">>},
{<<"origin">>,
<<"chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm">>},
{<<"user-agent">>,
<<"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/39.0.2171.65 Chrome/39.0.2171.65 Safari/537.36">>},
{<<"content-type">>,<<"text/plain;charset=UTF-8">>},
{<<"accept">>,<<"*/*">>},
{<<"accept-encoding">>,<<"gzip, deflate">>},
{<<"accept-language">>,<<"en-GB,en-US;q=0.8,en;q=0.6">>}]},
{p_headers,[{<<"connection">>,[<<"keep-alive">>]}]},
{cookies,undefined},
{meta,[]},
{body_state,waiting},
{buffer,<<"asdf">>},
{multipart,undefined},
{resp_compress,false},
{resp_state,waiting},
{resp_headers,[]},
{resp_body,<<>>},
{onresponse,undefined}]},
{state,{state}}],
[{cowboy_protocol,execute,4,[{file,"src/cowboy_protocol.erl"},{line,442}]}]}
=ERROR REPORT==== 19-Oct-2016::18:56:51 ===
Ranch listener my_http_listener had connection process started with cowboy_protocol:start_link/4 at <0.361.0> exit with reason:
{[{reason,undef},{mfa,{hello_handler,handle,2}},{stacktrace,[{hackney,start,[],[]},{hello_handler,handle,2,[{file,"src/hello_handler.erl"},{line,18}]},{cowboy_handler,handler_handle,4,[{file,"src/cowboy_handler.erl"},{line,111}]},{cowboy_protocol,execute,4,[{file,"src/cowboy_protocol.erl"},{line,442}]}]},{req,[{socket,#Port<0.267>},{transport,ranch_tcp},{connection,keepalive},{pid,<0.361.0>},{method,<<"POST">>},{version,'HTTP/1.1'},{peer,{{10,0,0,1},40049}},{host,<<"10.0.0.103">>},{host_info,undefined},{port,8080},{path,<<"/">>},{path_info,undefined},
{QS,&LT;&LT;&GT;&GT;},{qs_vals,未定义},{绑定,[]},{标头,[{&LT;&LT; “宿主” &GT;&GT;,&LT;&LT; “10.0.0.103:8080">>},{<<"connection">>,<<"keep-alive">>},{<<"content-length “&GT;&GT;,&LT;&LT;” 4 “&GT;&GT;},{&LT;&LT;” 缓存控制 “&GT;&GT;,&LT;&LT;” 无缓存“&GT;&GT;},{ &LT;&LT; “原点” &GT;&GT;,&LT;&LT; “铬扩展:// fdmmgilgnpjigdojojpjoooidkmcomcm” &GT;&GT;},{&LT;&LT; “用户代理” &GT;&GT;,&LT;&LT; “Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,与Gecko一样)Ubuntu Chromium / 39.0.2171.65 Chrome / 39.0.2171.65 Safari / 537.36”&gt;&gt;},{&lt;&lt;&lt;“content-type” &GT;&GT;,&LT;&LT; “text / plain的;字符集= UTF-8” &GT;&GT;},{&LT;&LT; “接受” &GT;&GT;,&LT;&LT;“ / “&gt;&gt;},{&lt;&lt;”accept-encoding“&gt;&gt;,&lt;&lt;”gzip,deflate“&gt;&gt;},{&lt;&lt;”accept-language“&gt ;&GT;,&LT;&LT; “EN-GB,EN-US; q = 0.8,连接; q = 0.6” &GT;&GT;}]},{p_headers,[{&LT;&LT; “连接” &GT;&GT ;,并[d&LT; “保持活动” &GT;&GT;]}]},{饼干,未定义},{元,[]},{body_state,等待},{buffe R,&LT;&LT; “ASDF” &GT;&GT;},{多,未定义},{resp_compress,FALSE},{resp_state,等待},{resp_headers,[]},{resp_body,&LT;&LT;&GT;&GT ;},{onresponse,未定义}]},{状态,{状态}}],[{cowboy_protocol,执行,如图4所示,[{文件, “SRC / cowboy_protocol.erl”},{线,442}]}]}
hello_handler.erl:
-module(hello_handler).
-behaviour(cowboy_http_handler).
-export([init/3]).
-export([handle/2]).
-export([terminate/3]).
-record(state, {
}).
init(_, Req, _Opts) ->
hackney:start(),
{ok, Req, #state{}}.
handle(Req, State) ->
{Method, Req2} = cowboy_req:method(Req),
case Method of
<<"POST">> ->
{ok, _, _, Ref} = hackney:request(get, <<"http://www.youtube.com">>,
[], <<>>, [{pool, default}]),
{ok, Body} = hackney:body(Ref),
io:format("body: ~p~n~n", [Body]),
ResponseBody = <<"Hello Erl POST!">>;
<<"GET">> ->
ResponseBody = <<"Hello Erlang1!">>
end,
{ok, Req2} = cowboy_req:reply(200,
[{<<"content-type">>, <<"text/plain">>}],
ResponseBody,
Req),
{ok, Req2, State}.
terminate(_Reason, _Req, _State) ->
ok.
答案 0 :(得分:0)
{[{reason,undef},
{mfa,{hello_handler,handle,2}},
{stacktrace,[{hackney,start,[],[]},
{hello_handler,handle,2,
[{file,"src/hello_handler.erl"},{line,18}]},
{cowboy_handler,handler_handle,4,
[{file,"src/cowboy_handler.erl"},{line,111}]},
{cowboy_protocol,execute,4,
[{file,"src/cowboy_protocol.erl"},
{line,442}]}]},
在cowboy_handler.erl 111行崩溃,https://github.com/ninenines/cowboy/blob/1.1.x/src/cowboy_handler.erl#L111
原因:hello_handler:handle / 2是undef
所以
确保src目录中的hello_handler.erl;
使用rebar compile
;
重启服务器或erlang shell中的l(hello_handler)