• Stars
    star
    1,322
  • Rank 35,547 (Top 0.8 %)
  • Language
    Erlang
  • License
    Other
  • Created over 12 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

simple HTTP client in Erlang

hackney - HTTP client library in Erlang

Copyright (c) 2012-2023 Benoรฎt Chesneau.

Version: 1.20.1

hackney

hackney is an HTTP client library for Erlang.

Build Status Hex pm

Main features:

  • no message passing (except for asynchronous responses): response is directly streamed to the current process and state is kept in a #client{} record.
  • binary streams
  • SSL support
  • Keepalive handling
  • basic authentication
  • stream the response and the requests
  • fetch a response asynchronously
  • multipart support (streamed or not)
  • chunked encoding support
  • Can send files using the sendfile API
  • Optional socket pool
  • REST syntax: hackney:Method(URL) (where a method can be get, post, put, delete, ...)

Supported versions of Erlang are 22.3 and above. It is reported to work with version from 19.3 to 21.3.

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

Useful modules are:

  • hackney: main module. It contains all HTTP client functions.

  • hackney_http: HTTP parser in pure Erlang. This parser is able to parse HTTP responses and requests in a streaming fashion. If not set it will be autodetected if it's a request or a response that's needed.

  • hackney_headers Module to manipulate HTTP headers.

  • hackney_cookie: Module to manipulate cookies.

  • hackney_multipart: Module to encode/decode multipart.

  • hackney_url: Module to parse and create URIs.

  • hackney_date: Module to parse HTTP dates.

Read the NEWS file to get the last changelog.

Installation

Download the sources from our Github repository

To build the application simply run 'rebar3 compile'.

To run tests run 'rebar3 eunit'. To generate doc, run 'rebar3 edoc'.

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:


$ ./rebar3 shell

It is suggested that you install rebar3 user-wide as described here. This fixes zsh (and maybe other shells) escript-related bugs. Also this should speed things up.

> application:ensure_all_started(hackney).
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(ClientRef).

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 hackney: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.

Note 2: using the with_body option will return the body directly instead of a reference.

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_ssl,
Host = << "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.

To get local and remote ip and port information of a connection:

> hackney:peername(ConnRef).
> hackney:sockname(ConnRef).

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 kept open until you close it exclusively.

Send a body

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

  • {form, PropList} : To send a form
  • {multipart, Parts} : to send your body using the multipart API. Parts follow this format:
    • eof: end the multipart request
    • {file, Path}: to stream a file
    • {file, Path, ExtraHeaders}: to stream a file
    • {file, Path, Name, ExtraHeaders} : to send a file with DOM element name and extra headers
    • {Name, Content}: to send a full part
    • {Name, Content, ExtraHeaders}: to send a full part
    • {mp_mixed, Name, MixedBoundary}: To notify we start a part with a a mixed multipart content
    • {mp_mixed_eof, MixedBoundary}: To notify we end a part with a a mixed multipart content
  • {file, File} : To send a file
  • Bin: To send a binary or an iolist

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:send_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 {follow_redirect, true} is passed to the request, you will receive the following messages on valid redirection:

  • {redirect, To, Headers}
  • {see_other, To, Headers} for status 303 and POST requests.

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

Use the default pool

Hackney uses socket pools to reuse connections globally. By default, hackney uses a pool named default. You may want to use different pools in your application which allows you to maintain a group of connections. To use a different pool, do the following:

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

By adding the tuple {pool, mypool} to the options, hackney will use the connections stored in that pool. The pool gets started automatically the first time it is used. You can also explicitly configure and start the pool like this:

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 disable 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 false. This means that hackney will not use socket pools unless specifically requested using the pool option as described above.

To disable socket pools for a single request, specify the option {pool, false}.

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 can`t 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 the location property of the client state.

ex:

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

Use SSL/TLS with self signed certificates

Hackney uses CA bundles adapted from Mozilla by certifi. Recognising an organisation specific (self signed) certificates is possible by providing the necessary ssl_options. Note that ssl_options overrides all options passed to the ssl module.

ex (>= Erlang 21):

CACertFile = <path_to_self_signed_ca_bundle>,
CrlCheckTimeout = 5000,
SSLOptions = [
{verify, verify_peer},
{versions, ['tlsv1.2']},
{cacertfile, CACertFile},
{crl_check, peer},
{crl_cache, {ssl_crl_cache, {internal, [{http, CrlCheckTimeout}]}}},
{customize_hostname_check,
  [{match_fun, public_key:pkix_verify_hostname_match_fun(https)}]}],

Method = get,
URL = "http://my-organisation/",
ReqHeaders = [],
ReqBody = <<>>,
Options = [{ssl_options, SSLoptions}],
{ok, S, H, Ref} = hackney:request(Method, URL, ReqHeaders,
                                  ReqBody, Options),

%% To provide client certificate:

CertFile = <path_to_client_certificate>,
KeyFile = <path_to_client_private_key>,
SSLOptions1 = SSLoptions ++ [
{certfile, CertFile},
{keyfile, KeyFile}
],
Options1 = [{ssl_options, SSLoptions1}],
{ok, S1, H1, Ref1} = hackney:request(Method, URL, ReqHeaders,
                                     ReqBody, Options1).

Proxy a connection

HTTP Proxy

To use an HTTP tunnel add the option {proxy, ProxyUrl} where ProxyUrl can 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.

Metrics

Hackney offers the following metrics

You can enable metrics collection by adding a mod_metrics entry to hackney's app config. Metrics are disabled by default. The module specified must have an API matching that of the hackney metrics module.

To use folsom, specify {mod_metrics, folsom}, or if you want to use exometer, specify{mod_metrics, exometer} and ensure that folsom or exometer is in your code path and has been started.

Generic Hackney metrics

Name Type Description
hackney.nb_requests counter Number of running requests
hackney.total_requests counter Total number of requests
hackney.finished_requests counter Total number of requests finished

Metrics per Hosts

Name Type Description
hackney.HOST.nb_requests counter Number of running requests
hackney.HOST.request_time histogram Request time
hackney.HOST.connect_time histogram Connect time
hackney.HOST.response_time histogram Response time
hackney.HOST.connect_timeout counter Number of connect timeout
hackney.HOST.connect_error counter Number of timeout errors
hackney_pool.HOST.new_connection counter Number of new pool connections per host
hackney_pool.HOST.reuse_connection counter Number of reused pool connections per host

Metrics per Pool

Name Type Description
hackney_pool.POOLNAME.take_rate meter meter recording rate at which a connection is retrieved from the pool
hackney_pool.POOLNAME.no_socket counter Count of new connections
hackney_pool.POOLNAME.in_use_count histogram How many connections from the pool are used
hackney_pool.POOLNAME.free_count histogram Number of free sockets in the pool
hackney_pool.POOLNAME.queue_count histogram queued clients

Contribute

For issues, comments or feedback please create an issue.

Notes for developers

If you want to contribute patches or improve the docs, you will need to build hackney using the rebar_dev.config file. It can also be built using the Makefile:

$ rebar3 update
$ rebar3 compile

For successfully running the hackney test suite locally it is necessary to install httpbin.

An example installation using virtualenv::

$ mkvirtualenv hackney
$ pip install gunicorn httpbin

Running the tests:

$ gunicorn --daemon --pid httpbin.pid httpbin:app
$ rebar3 eunit
$ kill `cat httpbin.pid`

Modules

hackney
hackney_app
hackney_bstr
hackney_connect
hackney_connection
hackney_connections
hackney_cookie
hackney_date
hackney_headers
hackney_headers_new
hackney_http
hackney_http_connect
hackney_local_tcp
hackney_manager
hackney_metrics
hackney_multipart
hackney_pool
hackney_pool_handler
hackney_request
hackney_response
hackney_socks5
hackney_ssl
hackney_stream
hackney_sup
hackney_tcp
hackney_trace
hackney_url
hackney_util

More Repositories

1

gunicorn

gunicorn 'Green Unicorn' is a WSGI HTTP Server for UNIX, fast clients and sleepy applications.
Python
9,757
star
2

restkit

an HTTP resource kit for Python
Python
405
star
3

offset

Python
377
star
4

gaffer

control, watch and launch your applications and jobs over HTTP.
Python
363
star
5

http-parser

HTTP request/response parser for python in C
C
337
star
6

erica

tool to manage couchapps
JavaScript
269
star
7

tproxy

simple TCP routing proxy
Python
265
star
8

couchdbkit

CouchDB python framework
Python
265
star
9

couchbeam

Apache CouchDB client in Erlang
Erlang
238
star
10

flower

collection of modules to build distributed and reliable concurrent systems in Python.
Python
207
star
11

socketpool

Generic socket pool
Python
163
star
12

cowboy_revproxy

simple TCP routing proxy (layer 7) in erlang
Erlang
86
star
13

econfig

simple Erlang config handler using INI files
Erlang
79
star
14

hooks

generic plugin & hook system for Erlang applications
Erlang
72
star
15

erlang-metrics

A generic interface to different metrics systems in Erlang.
Erlang
70
star
16

dj-webmachine

Django REST layer
Python
59
star
17

erlang-nat

implements NAT handling facilities for Erlang applications
Erlang
57
star
18

couchdbproxy

Simple multinode couchdb proxy
Erlang
57
star
19

dnssd_erlang

Erlang interface to Apple's Bonjour DNS Service Discovery implementation
Erlang
46
star
20

nat_upnp

Erlang library to map your internal port to an external using UNP IGD
Erlang
42
star
21

uzmq

libuv interface for ZeroMQ
Python
34
star
22

ejson

EJSON - decode and encode JSON into/from Erlang terms (from CouchDB project)
C
32
star
23

mochicow

mochiweb adapter for cowboy.
Erlang
29
star
24

dj-revproxy

simple reverse proxy for django.
Python
28
star
25

hroute

simple HTTP proxy based on tproxy
Python
28
star
26

inet_cidr

CIDR erlang library
Erlang
27
star
27

mimerl

library to handle mimetypes
Erlang
27
star
28

sieve

sieve is a simple TCP routing proxy (layer 7) in erlang
Erlang
26
star
29

opencouch

A embeddable document oriented database compatible with Apache CouchDB
Erlang
22
star
30

gunicorn-recipes

Collection of recipe and examples to help in gunicorn deployement, installation and configuration.
19
star
31

nymphormation

Nymphormation is a Couchapp that allow people to share links or news .
JavaScript
19
star
32

rebar3_path_deps

A rebar plugin to specify path dependencies.
Erlang
19
star
33

couch_zmq

zeromq endpoint for couchdb.
Erlang
18
star
34

noddycouch

minimal couchdb toolkit for nodejs.
JavaScript
17
star
35

afgwardiary

couchapp to render afgwardiary data from wikileaks
JavaScript
16
star
36

upnp

Erlang UPNP Module
Erlang
14
star
37

couchc

minimal couchdb internal API wrapper
Erlang
14
star
38

couchapp-ng

Couchapp Engine
Erlang
14
star
39

couchdb

My CouchDB hack repository
JavaScript
14
star
40

cbt

multi-layer MVCC log append-only database library based on the Apache CouchDB btree.
Erlang
14
star
41

inet_ext

inet extensions library
Erlang
13
star
42

hackney_lib

WEB toolkit including miscellaneous modules to play with HTTP and Web protocols
Erlang
11
star
43

memdb

Erlang memory backend K/V store
Erlang
10
star
44

rebar3_protobuffs

rebar3 protobuffs provider using protobuffs from Basho
Erlang
10
star
45

unicode_util_compat

unicode_util compatibility library for Erlang < 20
Erlang
10
star
46

fserve

simple file server in python on unix systems with sendfile support using pistil
Python
8
star
47

pywebmachine

Python port of Basho's WebMachine
Python
8
star
48

hypercouch

Full text indexing of CouchDB via HyperEstraier
Python
7
star
49

natpmp

Erlang Nat-PMP client
Erlang
7
star
50

rcouch_template

Rebar templates for generating custom couchdb releases
Shell
7
star
51

dotfiles

repository of dotifile in my home
Vim Script
6
star
52

hackney_disp

Load-balanced Pool dispatcher based on dispcount for hackney.
Erlang
6
star
53

benoitc.org

My own site
JavaScript
6
star
54

couchdb_internals

Miscelleaneous docs about the couchdb protocol and couchdb implementation
6
star
55

dcouch

an Alternative to Mnesia with Unique Features
5
star
56

backbone.py

just some code to initiate construction of objects from any remote repo.
Python
5
star
57

nplib

library collecting modules to decode a bunch of network protocoles
Erlang
5
star
58

qrkit

simple and stupid qr code binding extracted from qrurl
Python
5
star
59

couchdocs

Documentation for CouchDB
Objective-C
5
star
60

lhttpc-old

GIT clone of http://bitbucket.org/etc/lhttpc/
Erlang
4
star
61

blanket

couchdb/refuge client.
4
star
62

couch_randomdoc

simple couchdb module to add support of random document fetching.
Erlang
4
star
63

erlang-pbkdf2-nif

PBKDF2 NIF implementation
C
4
star
64

couchdb-old

Couchdb mirror repo to handle some custom dev
Erlang
4
star
65

rebar3_cargo

Erlang
4
star
66

erl_stun

Erlang
4
star
67

hackney_pooler

Experiment an API to limit the number of hackney requests launched concurrently
Erlang
4
star
68

emonk_helper

Some extensiosn to ease the use of emonk (http://github.com/davisp/emonk)
Erlang
3
star
69

couchjs

rebar couchjs version for CouchDB
JavaScript
3
star
70

dj-cookieauth

Secure Cookie Auth module for Django
Python
3
star
71

cablesgate

Script to put wikileaks diploamatic cables in a couchdb
Python
3
star
72

echohttp

simple service echoing any requests made to http://echohttp.com/echo
Erlang
3
star
73

mt-compono

minimalist cms using Django
JavaScript
3
star
74

dj-pages

minimal content cms/renderer
JavaScript
3
star
75

build-android

Shell
3
star
76

vrac

Simple app to manage all data fragments in vurt
JavaScript
3
star
77

erlang-idna-nif

C
3
star
78

esync

simple tool to sync files
Erlang
3
star
79

epygments

Simple wrapper to Pygments - prettify source code in your erlang program.
Erlang
3
star
80

libcouch

Pure Erlang CouchDB Store library.
Erlang
3
star
81

overlay

CSS
3
star
82

buildout_couchdb

zc buildout recipe for Apache CouchDB server
Python
3
star
83

dummy_app

Erlang
2
star
84

hello_world

Erlang
2
star
85

wineoverip

JavaScript
2
star
86

foodoverip

Handle #foodoverip
JavaScript
2
star
87

couchit_experiment

Standalone couch.it powered by CouchDB
Erlang
2
star
88

benoitc.github.com

Github Homepage
2
star
89

dataporn

intensive data usage
2
star
90

qrurl

C
2
star
91

enki

Document-oriented database framework
2
star
92

mod_percept2

percept2 ejabberd module
JavaScript
1
star
93

tiptoe

1
star
94

mz_counter

simple atomic counter for erlang applications
C
1
star
95

enki--multimedia.org

1
star
96

unicode_extra

Extra function not available in Erlang unicode_util module
Erlang
1
star
97

erlang-ucs

1
star
98

osdc2013_demo

demo for OSDC 2013
Erlang
1
star