erlang 无法向Cowboy请求

pvabu6sv  于 2023-03-06  发布在  Erlang
关注(0)|答案(1)|浏览(242)

我根本不能用Erlang/Cowboy发出请求。我可以从erlang shell发出请求,但运行cowboy版本时不行。我也试过使用'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,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}]}]}

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.
mcdcgff0

mcdcgff01#

{[{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}]}]},

在www.example.com的cowboy_handler.erl 111行发生崩溃https://github.com/ninenines/cowboy/blob/1.1.x/src/cowboy_handler.erl#L111
原因:hello_handler:句柄/2未定义
所以
1.确保源目录中有hello_handler.erl;
1.用rebar compile编译;
1.在erlang shell中重新启动服务器或l(hello_handler)

相关问题