Showing debug trace information for hackney (HTTPoison) in IEx

Using IEx with a remote API with HTTPoison, it can be handy to show int he console the request and response you get back with hackney.

You can do this by entering the following in iex:

:hackney_trace.enable(:max, :io)

You can also add this to .iex.exs file so it’ll always be added to your iex sessions by default so you don’t need to always retype it when needed.

This will give you an output like this:

iex(1)> HTTPoison.get("https://jsonplaceholder.typicode.com/todos/1")
[hackney trace 80 <0.208.0> 2020:01:10 11:59:45 4110] request 
   Content: [{module,hackney},
             {line,311},
             {method,get},
             {url,{hackney_url,hackney_ssl,https,
                               <<"jsonplaceholder.typicode.com">>,
                               <<"/todos/1">>,<<"/todos/1">>,<<>>,<<>>,
                               "jsonplaceholder.typicode.com",443,<<>>,<<>>}},
             {headers,[]},
             {body,<<>>},
             {options,[]}]
[hackney trace 60 <0.208.0> 2020:01:10 11:59:45 4110] request without proxy 
   Content: [{module,hackney},{line,683}]
[hackney trace 60 <0.208.0> 2020:01:10 11:59:45 4110] connect 
   Content: [{module,hackney_connect},
             {line,35},
             {transport,hackney_ssl},
             {host,"jsonplaceholder.typicode.com"},
             {port,443},
             {dynamic,true}]
[hackney trace 80 <0.208.0> 2020:01:10 11:59:45 4111] no socket in the pool 
   Content: [{module,hackney_connect},{line,230},{pool,default}]
[hackney trace 80 <0.208.0> 2020:01:10 11:59:45 4481] new connection 
   Content: [{module,hackney_connect},{line,279}]
[hackney trace 40 <0.208.0> 2020:01:10 11:59:45 4481] perform request 
   Content: [{module,hackney_request},
             {line,102},
             {header_data,[<<"GET /todos/1 HTTP/1.1\r\n">>,
                           [[<<"Host">>,": ",
                             <<"jsonplaceholder.typicode.com">>,"\r\n"],
                            [<<"User-Agent">>,": ",<<"hackney/1.15.2">>,
                             "\r\n"],
                            "\r\n"]]},
             {perform_all,true},
             {expect,false}]
[hackney trace 80 <0.208.0> 2020:01:10 11:59:45 4530] got response 
   Content: [{module,hackney},
             {line,379},
             {response,
                 {ok,200,
                     [{<<"Date">>,<<"Fri, 10 Jan 2020 12:00:32 GMT">>},
                      {<<"Content-Type">>,
                       <<"application/json; charset=utf-8">>},
                      {<<"Content-Length">>,<<"83">>},
                      {<<"Connection">>,<<"keep-alive">>},
                      {<<"Set-Cookie">>,
                       <<"__cfduid=d57cd6aa517712b71a7bdc8b947d36ad31578657632; expires=Sun, 09-Feb-20 12:00:32 GMT; path=/; domain=.typicode.com; HttpOnly; SameSite=Lax">>},
                      {<<"X-Powered-By">>,<<"Express">>},
                      {<<"Vary">>,<<"Origin, Accept-Encoding">>},
                      {<<"Access-Control-Allow-Credentials">>,<<"true">>},
                      {<<"Cache-Control">>,<<"max-age=14400">>},
                      {<<"Pragma">>,<<"no-cache">>},
                      {<<"Expires">>,<<"-1">>},
                      {<<"X-Content-Type-Options">>,<<"nosniff">>},
                      {<<"Etag">>,<<"W/\"53-hfEnumeNh6YirfjyjaujcOPPT+s\"">>},
                      {<<"Via">>,<<"1.1 vegur">>},
                      {<<"CF-Cache-Status">>,<<"HIT">>},
                      {<<"Age">>,<<"6237">>},
                      {<<"Accept-Ranges">>,<<"bytes">>},
                      {<<"Expect-CT">>,
                       <<"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"">>},
                      {<<"Server">>,<<"cloudflare">>},
                      {<<"CF-RAY">>,<<"552e7dbadb66df30-MEL">>}],
                     {client,
                         {1578,657585,110420},
                         {metrics_ng,metrics_dummy},
                         hackney_ssl,"jsonplaceholder.typicode.com",443,
                         <<"jsonplaceholder.typicode.com">>,[],
                         {sslsocket,
                             {gen_tcp,#Port<0.27>,tls_connection,undefined},
                             [<0.257.0>,<0.256.0>]},
                         {default,#Ref<0.437769858.2554068993.81094>,
                             {"jsonplaceholder.typicode.com",443,hackney_ssl},
                             <0.234.0>,hackney_ssl},
                         #Ref<0.437769858.2554068993.81094>,true,hackney_pool,
                         5000,false,5,false,5,nil,nil,
                         {hparser,response,4096,10,0,on_body,
                             <<"{\n  \"userId\": 1,\n  \"id\": 1,\n  \"title\": \"delectus aut autem\",\n  \"completed\": false\n}">>,
                             {1,1},
                             <<>>,[],83,<<>>,<<"keep-alive">>,
                             <<"application/json; charset=utf-8">>,<<>>,
                             waiting},
                         {20,
                          {dict,20,16,16,8,80,48,
                              {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],
                               []},
                              {{[[<<"content-length">>,
                                  {2,<<"Content-Length">>,<<"83">>}],
                                 [<<"accept-ranges">>,
                                  {16,<<"Accept-Ranges">>,<<"bytes">>}]],
                                [],
                                [[<<"date">>,
                                  {0,<<"Date">>,
                                   <<"Fri, 10 Jan 2020 12:00:32 GMT">>}],
                                 [<<"x-content-type-options">>,
                                  {11,<<"X-Content-Type-Options">>,
                                   <<"nosniff">>}],
                                 [<<"cf-cache-status">>,
                                  {14,<<"CF-Cache-Status">>,<<"HIT">>}],
                                 [<<"expect-ct">>,
                                  {17,<<"Expect-CT">>,
                                   <<"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"">>}],
                                 [<<"cf-ray">>,
                                  {19,<<"CF-RAY">>,
                                   <<"552e7dbadb66df30-MEL">>}]],
                                [[<<"expires">>,{10,<<"Expires">>,<<"-1">>}]],
                                [],
                                [[<<"etag">>,
                                  {12,<<"Etag">>,
                                   <<"W/\"53-hfEnumeNh6YirfjyjaujcOPPT+s\"">>}],
                                 [<<"server">>,
                                  {18,<<"Server">>,<<"cloudflare">>}]],
                                [[<<"vary">>,
                                  {6,<<"Vary">>,
                                   <<"Origin, Accept-Encoding">>}]],
                                [[<<"cache-control">>,
                                  {8,<<"Cache-Control">>,
                                   <<"max-age=14400">>}]],
                                [],
                                [[<<"x-powered-by">>,
                                  {5,<<"X-Powered-By">>,<<"Express">>}]],
                                [[<<"content-type">>,
                                  {1,<<"Content-Type">>,
                                   <<"application/json; charset=utf-8">>}],
                                 [<<"pragma">>,
                                  {9,<<"Pragma">>,<<"no-cache">>}],
                                 [<<"age">>,{15,<<"Age">>,<<"6237">>}]],
                                [[<<"access-control-allow-credentials">>,
                                  {7,<<"Access-Control-Allow-Credentials">>,
                                   <<"true">>}]],
                                [[<<"connection">>,
                                  {3,<<"Connection">>,<<"keep-alive">>}]],
                                [[<<"set-cookie">>,
                                  {4,<<"Set-Cookie">>,
                                   <<"__cfduid=d57cd6aa517712b71a7bdc8b947d36ad31578657632; expires=Sun, 09-Feb-20 12:00:32 GMT; path=/; domain=.typicode.com; HttpOnly; SameSite=Lax">>}],
                                 [<<"via">>,{13,<<"Via">>,<<"1.1 vegur">>}]],
                                [],[]}}}},
                         connected,waiting,nil,normal,false,false,false,
                         undefined,false,fun hackney_request:send/2,waiting,
                         nil,4096,<<>>,[],
                         {1,1},
                         83,nil,nil,<<"GET">>,<<"/todos/1">>,nil}}},
             {client,
                 {client,
                     {1578,657585,110420},
                     {metrics_ng,metrics_dummy},
                     hackney_ssl,"jsonplaceholder.typicode.com",443,
                     <<"jsonplaceholder.typicode.com">>,[],
                     {sslsocket,
                         {gen_tcp,#Port<0.27>,tls_connection,undefined},
                         [<0.257.0>,<0.256.0>]},
                     {default,#Ref<0.437769858.2554068993.81094>,
                         {"jsonplaceholder.typicode.com",443,hackney_ssl},
                         <0.234.0>,hackney_ssl},
                     #Ref<0.437769858.2554068993.81094>,true,hackney_pool,
                     5000,false,5,false,5,nil,nil,nil,
                     {0,
                      {dict,0,16,16,8,80,48,
                          {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
                          {{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],
                            []}}}}, 
                     connected,start,nil,normal,false,false,false,undefined,
                     false,nil,waiting,nil,4096,<<>>,[],undefined,nil,nil,nil,
                     nil,undefined,nil}}]
{:ok,
 %HTTPoison.Response{
   body: "{\n  \"userId\": 1,\n  \"id\": 1,\n  \"title\": \"delectus aut autem\",\n  \"completed\": false\n}",
   headers: [
     {"Date", "Fri, 10 Jan 2020 12:00:32 GMT"},
     {"Content-Type", "application/json; charset=utf-8"},
     {"Content-Length", "83"},
     {"Connection", "keep-alive"},
     {"Set-Cookie",
      "__cfduid=d57cd6aa517712b71a7bdc8b947d36ad31578657632; expires=Sun, 09-Feb-20 12:00:32 GMT; path=/; domain=.typicode.com; HttpOnly; SameSite=Lax"},
     {"X-Powered-By", "Express"},
     {"Vary", "Origin, Accept-Encoding"},
     {"Access-Control-Allow-Credentials", "true"},
     {"Cache-Control", "max-age=14400"},
     {"Pragma", "no-cache"},
     {"Expires", "-1"},
     {"X-Content-Type-Options", "nosniff"},
     {"Etag", "W/\"53-hfEnumeNh6YirfjyjaujcOPPT+s\""},
     {"Via", "1.1 vegur"},
     {"CF-Cache-Status", "HIT"},
     {"Age", "6237"},
     {"Accept-Ranges", "bytes"},
     {"Expect-CT",
      "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""},
     {"Server", "cloudflare"},
     {"CF-RAY", "552e7dbadb66df30-MEL"}
   ],
   request: %HTTPoison.Request{
     body: "",
     headers: [],
     method: :get,
     options: [],
     params: %{},
     url: "https://jsonplaceholder.typicode.com/todos/1"
   },
   request_url: "https://jsonplaceholder.typicode.com/todos/1",
   status_code: 200
 }}