This guide walks you through installing hackney and making your first HTTP requests.
Add hackney to your rebar.config:
{deps, [hackney]}.Add to your mix.exs:
{:hackney, "~> 4.0"}hackney is an OTP application. Start it before making requests:
application:ensure_all_started(hackney).{ok, 200, Headers, Ref} = hackney:get(<<"https://httpbin.org/get">>).{ok, Body} = hackney:body(Ref).Use with_body to get the body directly:
{ok, 200, Headers, Body} = hackney:get(URL, [], <<>>, [with_body]).URL = <<"https://httpbin.org/post">>,
Headers = [{<<"content-type">>, <<"application/json">>}],
Body = <<"{\"name\": \"hackney\"}">>,
{ok, 200, _, Ref} = hackney:post(URL, Headers, Body).hackney:post(URL, [], {form, [{<<"key">>, <<"value">>}]}).hackney:post(URL, [], {multipart, [
{<<"field">>, <<"value">>},
{file, <<"/path/to/file.txt">>}
]}).| Option | Description |
|---|---|
with_body |
Return body in response tuple |
{pool, Name} |
Use named connection pool |
{pool, false} |
Don't use pooling |
{connect_timeout, Ms} |
Connection timeout (default: 8000) |
{recv_timeout, Ms} |
Response timeout (default: 5000) |
async |
Receive response as messages |
{follow_redirect, true} |
Follow redirects |
insecure |
Skip SSL verification |
hackney pools connections by default:
%% Create a pool
hackney_pool:start_pool(api_pool, [{max_connections, 50}]).
%% Use the pool
hackney:get(URL, [], <<>>, [{pool, api_pool}]).
%% Disable pooling
hackney:get(URL, [], <<>>, [{pool, false}]).case hackney:get(URL) of
{ok, Status, Headers, Ref} ->
{ok, Body} = hackney:body(Ref);
{error, timeout} ->
handle_timeout();
{error, Reason} ->
handle_error(Reason)
end.- HTTP Guide - Streaming, async responses, advanced features
- WebSocket Guide - Real-time bidirectional communication
- Migration Guide - Upgrading from hackney 1.x