hackney - HTTP client library in Erlang

Copyright (c) 2012-2014 Benoît Chesneau.

Version: 0.15.0

hackney

hackney is an HTTP client library for Erlang.

Main features:

Hackney uses hackney_lib to handle any web protocols. If you want to manipulate headers, cookies, multipart or any other thing related to web protocols this is the place to go.

Note: This is a work in progress, see the TODO for more information on what still needs to be done.

Useful modules are:

Read the NEWS file to get the last changelog.

Installation

Download the sources from our Github repository

To build the application simply run ‘make’. This should build .beam, .app files and documentation.

To run tests run ‘make test’. To generate doc, run ‘make doc’.

Or add it to your rebar config


{deps, [
    ....
    {hackney, ".*", {git, "git://github.com/benoitc/hackney.git", {branch, "master"}}}
]}.

Basic usage

The basic usage of hackney is:

Start hackney

hackney is an OTP application. You have to start it first before using any of the functions. The hackney application will start the default socket pool for you.

To start in the console run:


$ erl -pa ebin -pa deps/*/ebin
1>> hackney:start().
ok

It will start hackney and all of the application it depends on:


application:start(crypto),
application:start(public_key),
application:start(ssl),
application:start(hackney).

Or add hackney to the applications property of your .app in a release

Simple request

Do a simple request that will return a client state:


Method = get,
URL = <<"https://friendpaste.com">>,
Headers = [],
Payload = <<>>,
Options = [],
{ok, StatusCode, RespHeaders, ClientRef} = hackney:request(Method, URL,
                                                        Headers, Payload,
                                                        Options).

The request method returns the tuple {ok, StatusCode, Headers, ClientRef} or {error, Reason}. A ClientRef is simply a reference to the current request that you can reuse.

If you prefer the REST syntax, you can also do:

hackney:Method(URL, Headers, Payload, Options)

where Method, can be any HTTP method in lowercase.

Read the body

{ok, Body} = hackney:body(Client).

hackney:body/1 fetch the body. To fetch it by chunk you can use the hackney:stream_body/1 function:


read_body(MaxLength, Ref, Acc) when MaxLength > byte_size(Acc) ->
    case stream_body(Ref) of
        {ok, Data} ->
            read_body(MaxLength, Ref, << Acc/binary, Data/binary >>);
        done ->
            {ok, Acc};
        {error, Reason} ->
            {error, Reason}
    end.

Note: you can also fetch a multipart response using the functions hackney:stream_multipart/1 and hackney:skip_multipart/1.

Reuse a connection

By default all connections are created and closed dynamically by hackney but sometimes you may want to reuse the same reference for your connections. It’s especially useful if you just want to handle serially a couple of requests.

A closed connection will automatically be reconnected.

To create a connection:


Transport = hackney_tcp_transport,
Host = << "https://friendpaste.com" >>,
Port = 443,
Options = [],
{ok, ConnRef} = hackney:connect(Transport, Host, Port, Options)

To create a connection that will use an HTTP proxy use hackney_http_proxy:connect_proxy/5 instead.

Make a request

Once you created a connection use the hackney:send_request/2 function to make a request:


ReqBody = << "{ \"snippet\": \"some snippet\" }" >>,
ReqHeaders = [{<<"Content-Type">>, <<"application/json">>}],
NextPath = <<"/">>,
NextMethod = post,
NextReq = {NextMethod, NextPath, ReqHeaders, ReqBody}
{ok, _, _, ConnRef} = hackney:send_request(ConnRef, NextReq).
{ok, Body1} = hackney:body(ConnRef),

Here we are posting a JSON payload to ‘/‘ on the friendpaste service to create a paste. Then we close the client connection.

If your connection supports keepalive the connection will be simply :

Send a body

hackney helps you send different payloads by passing different terms as the request body:

Note: to send a chunked request, just add the Transfer-Encoding: chunked header to your headers. Binary and Iolist bodies will be then sent using the chunked encoding.

Send the body by yourself

While the default is to directly send the request and fetch the status and headers, if the body is set as the atom stream the request and send_request function will return {ok, Client}. Then you can use the function hackney:send_body/2 to stream the request body and hackney:start_response/1 to initialize the response.

Note: The function hackney:start_response/1 will only accept a Client that is waiting for a response (with a response state equal to the atom waiting).

Ex:


ReqBody = << "{
      \"id\": \"some_paste_id2\",
      \"rev\": \"some_revision_id\",
      \"changeset\": \"changeset in unidiff format\"
}" >>,
ReqHeaders = [{<<"Content-Type">>, <<"application/json">>}],
Path = <<"https://friendpaste.com/">>,
Method = post,
{ok, ClientRef} = hackney:request(Method, Path, ReqHeaders, stream, []),
ok  = hackney:send_body(ClientRef, ReqBody),
{ok, _Status, _Headers, ClientRef} = hackney:start_response(ClientRef),
{ok, Body} = hackney:body(ClientRef),

Note: to send a multipart body in a streaming fashion use the hackney:sen_multipart_body/2 function.

Get a response asynchronously

Since the 0.6 version, hackney is able to fetch the response asynchronously using the async option:


Url = <<"https://friendpaste.com/_all_languages">>,
Opts = [async],
LoopFun = fun(Loop, Ref) ->
        receive
            {hackney_response, Ref, {status, StatusInt, Reason}} ->
                io:format("got status: ~p with reason ~p~n", [StatusInt,
                                                              Reason]),
                Loop(Loop, Ref);
            {hackney_response, Ref, {headers, Headers}} ->
                io:format("got headers: ~p~n", [Headers]),
                Loop(Loop, Ref);
            {hackney_response, Ref, done} ->
                ok;
            {hackney_response, Ref, Bin} ->
                io:format("got chunk: ~p~n", [Bin]),
                Loop(Loop, Ref);

            Else ->
                io:format("else ~p~n", [Else]),
                ok
        end
    end.

{ok, ClientRef} = hackney:get(Url, [], <<>>, Opts),
LoopFun(LoopFun, ClientRef).

Note 1: When {async, once} is used the socket will receive only once. To receive the other messages use the function hackney:stream_next/1.

Note 2: Asynchronous responses automatically checkout the socket at the end.

Note 3: At any time you can go back and receive your response synchronously using the function hackney:stop_async/1 See the example test_async_once2 for the usage.

Note 4: When the option {following_redirect, true} is passed to the request, you will receive the folllowing messages on valid redirection:

Note 5: You can send the messages to another process by using the option {stream_to, Pid} .

Use the default pool

To reuse a connection globally in your application you can also use a socket pool. On startup, hackney launches a pool named default. To use it do the following:


Method = get,
URL = <<"https://friendpaste.com">>,
Headers = [],
Payload = <<>>,
Options = [{pool, default}],
{ok, StatusCode, RespHeaders, ClientRef} = hackney:request(Method, URL, Headers,
                                                        Payload, Options).

By adding the tuple {pool, default} to the options, hackney will use the connections stored in that pool.

You can also use different pools in your application which allows you to maintain a group of connections.


PoolName = mypool,
Options = [{timeout, 150000}, {max_connections, 100}],
ok = hackney_pool:start_pool(PoolName, Options),

timeout is the time we keep the connection alive in the pool, max_connections is the number of connections maintained in the pool. Each connection in a pool is monitored and closed connections are removed automatically.

To close a pool do:

hackney_pool:stop_pool(PoolName).

Note: Sometimes you want to always use the default pool in your app without having to set the client option each time. You can now do this by setting the hackney application environment key use_default_pool to true.

Use a custom pool handler.

Since the version 0.8 it is now possible to use your own Pool to maintain the connections in hackney.

A pool handler is a module that handles the hackney_pool_handler behaviour.

See for example the hackney_disp a load-balanced Pool dispatcher based on dispcount].> Note: for now you cant force the pool handler / client. ### Automatically follow a redirection If the option{follow_redirect, true}is given to the request, the client will be able to automatically follow the redirection and retrieve the body. The maximum number of connections can be set using the{max_redirect, Max}option. Default is 5. The client will follow redirects on 301, 302 & 307 if the method is get or head. If another method is used the tuple{ok, maybe_redirect, Status, Headers, Client}will be returned. It will only follow 303 redirects (see other) if the method is a POST. Last Location is stored in thelocationproperty of the client state. ex: ```erlang Method = get, URL = "http://friendpaste.com/", ReqHeaders = [{<<"accept-encoding">>, <<"identity">>}], ReqBody = <<>>, Options = [{follow_redirect, true}, {max_redirect, 5}], {ok, S, H, Ref} = hackney:request(Method, URL, ReqHeaders, ReqBody, Options), {ok, Body1} = hackney:body(Ref). ``` ### Proxy a connection #### HTTP Proxy To use an HTTP tunnel add the option{proxy, ProxyUrl}whereProxyUrlcan be a simple url or an{Host, Port}tuple. If you need to authenticate set the option{proxy_auth, {User, Password}}. #### SOCKS5 proxy Hackney supports the connection via a socks5 proxy. To set a socks5 proxy, use the following settings: -{proxy, {socks5, ProxyHost, ProxyPort}}: to set the host and port of the proxy to connect. -{socks5_user, Username}: to set the user used to connect to the proxy -{socks5_pass, Password}: to set the password used to connect to the proxy SSL and TCP connections can be forwarded via a socks5 proxy. hackney is automatically upgrading to an SSL connection if needed. ## Contribute For issues, comments or feedback please [create an issue](http://github.com/benoitc/hackney/issues). ### Notes for developers If you want to contribute patches or improve the docs, you will need to build hackney using therebar_dev.config` file. It can also be built using the Makefile: sh $ make dev ; # compile & get deps $ make devclean ; # clean all files ## Modules ## <table width=”100%” border=”0” summary=”list of modules”> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney.md” class=”module”>hackney</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_app.md” class=”module”>hackney_app</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_bstr.md” class=”module”>hackney_bstr</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_connect.md” class=”module”>hackney_connect</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_cookie.md” class=”module”>hackney_cookie</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_date.md” class=”module”>hackney_date</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_deps.md” class=”module”>hackney_deps</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_headers.md” class=”module”>hackney_headers</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_http.md” class=”module”>hackney_http</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_http_connect.md” class=”module”>hackney_http_connect</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_idna.md” class=”module”>hackney_idna</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_manager.md” class=”module”>hackney_manager</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_mimetypes.md” class=”module”>hackney_mimetypes</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_multipart.md” class=”module”>hackney_multipart</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_pool.md” class=”module”>hackney_pool</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_pool_handler.md” class=”module”>hackney_pool_handler</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_request.md” class=”module”>hackney_request</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_response.md” class=”module”>hackney_response</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_socks5.md” class=”module”>hackney_socks5</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_ssl_transport.md” class=”module”>hackney_ssl_transport</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_stream.md” class=”module”>hackney_stream</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_sup.md” class=”module”>hackney_sup</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_tcp_transport.md” class=”module”>hackney_tcp_transport</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_url.md” class=”module”>hackney_url</a></td></tr> <tr><td><a href=”http://github.com/benoitc/hackney/blob/master/doc/hackney_util.md” class=”module”>hackney_util</a></td></tr></table>