根本不能向Erlang/牛仔提出要求。我可以用erlang壳做一个,但在运行牛仔版本时不行。还尝试使用“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,<<>>},{qs_vals,未定义},{绑定,[],{标头,{<<“主机”>>,<<"10.0.0.103:8080">>},{<<“连接”>>,<<“保持活着的”>>},{<<“内容长度”>>,<<"4">>},{<<“缓存-控制”>>,<<“no-cache”}},{en20“原产地”,{<<“用户代理”>>,<<"Mozilla/5.0“(X11;Linux x86_64):AppleWebKit/537.36 (KHTML,类似Gecko) Ubuntu /39.0.2171.65 Chrome/39.0.2171.65 Safari/537.36">>},{<<“内容类型”>>,<<“文本/普通;charset=UTF-8”>>},{<<“接受”>>,<<"/">>},{<<“接受-编码”>>,<<"gzip,缩减“>>}”,{<<-语言“>>,<<"en-GB,en-US;q=0.8,en;Q=0.6“>>},{p_headers,[{<<”连接“>>,<<”保持存活“>>}]},{cookie,[]},{meta,[]},{body_state,等待},{缓冲区,<<"asdf">>},{多部分,未定义},{resp_compress,false},{resp_state,等待},{resp_headers,[]},{resp_body,<<>>},{onresponse,未定义},{state,{state}},[{cowboy_protocol,execute,4,{file,“src/牛仔协议.src”},{line,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.
发布于 2016-10-20 07:09:50
{[{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号线坠毁,handler.erl#L111
原因:handler:句柄/2是undef
所以
rebar compile
编译;l(hello_handler)
https://stackoverflow.com/questions/40139877
复制相似问题