2016-10-19 1 views
0

Impossible de faire une demande avec Erlang/Cowboy. Je peux en faire un depuis le shell erlang mais pas lors de la sortie d'un cow-boy. Essayé en utilisant la bibliothèque 'hackney' ainsi:Erlang Problèmes HTTP

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]), 

erreur:

Error in process <0.361.0> on node '[email protected]' 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, indéfinies}, {reliures, []}, {têtes , [{< < "hôte" >>, < < "10.0.0.103:8080" >>}, {< < "connexion" >>, < < "keep-alive" >>}, {< < « Content- longueur ">>, < <" 4 ">>, {< < "cache-control" >>, < < "no-cache" >>}, {< < "origine" >>, < < "chrome-extension: // fdmmgilgnpjigdojojpjoooidkmcomcm" >>}, {< < « utilisateur -agent ">>, < <" Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, comme Gecko) Ubuntu Chromium/39.0.2171.65 Chrome/39.0.2171.65 Safari/537.36 ">>}, {< <" type-contenu ">>, < <" text/plain; charset = UTF-8 ">>}, {< < "accepter" >>, < <" / « >>}, {< < "Accept-Encoding" >>, < < "gzip, dégonfler" >>} , {< < "accept-language" >>, < < "en-GB, en-US; q = 0,8, en; q = 0,6" >>}}, {p_headers, [{< < "connexion"> >, [< < "keep-alive" >>]}]}, {cookies, non défini}, {meta, []}, {body_state, en attente}, {buffer, < < "asdf" >>}, {multipart, indéfini}, {resp_compress, false}, {resp_state, en attente}, {resp_headers, []}, {resp_body, < < >>}, {onresponse, indéfini}]} , {état, {état}}], [{cowboy_protocol, exécuter, 4, [{fichier "src/cowboy_protocol.erl"}, {ligne 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

Pouvez-vous poster le contenu de 'src/hello_handler.erl' et' src/cowboy_handler.erl'? – Dogbert

+0

Bonjour gestionnaire est trop long pour entrer dans un commentaire, cowboy_handler fait partie du cadre cowboy – Yoker

+0

S'il vous plaît ajouter 'hello_handler.erl' à la question (vous pouvez modifier la question).Il semble qu'il vous manque la fonction 'handle/2' dans ce module. – Dogbert

Répondre

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

collision à la ligne cowboy_handler.erl 111, https://github.com/ninenines/cowboy/blob/1.1.x/src/cowboy_handler.erl#L111

Motif: hello_handler: poignée/2 est undef

Alors

  1. Assurez-vous que votre hello_handler.erl dans src dir;

  2. Compilez-le avec rebar compile;

  3. restart server ou l(hello_handler) en Erlang shell