gun - Asynchronous HTTP client


The gun module provides an asynchronous interface for connecting and communicating with Web servers over HTTP, HTTP/2 or Websocket.









Gun will inform the calling process of events asynchronously by sending any of the following messages:




The response messages will be sent to the process that opened the connection by default. The reply_to request option can be used to redirect request-specific messages to a different process.



connect_destination() :: #{
    host      := inet:hostname() | inet:ip_address(),
    port      := inet:port_number(),

    username  => iodata(),
    password  => iodata(),
    protocols => [http | http2],
    transport => tcp | tls,

    tls_opts              => [ssl:connect_option()],
    tls_handshake_timeout => timeout()

Destination of a CONNECT request.

The default value, if any, is given next to the option name:

host, port

Destination hostname and port number. Mandatory.

Upon successful completion of the CONNECT request, Gun will begin using these as the host and port of the origin server for subsequent requests.

username, password

Proxy authorization credentials. They are only sent when both options are provided.

protocol (http)

Protocol that will be used for tunneled requests.

transport (tcp)

Transport that will be used for tunneled requests. Note that due to Erlang/OTP limitations it is not possible to tunnel a TLS connection inside a TLS tunnel.

tls_opts ([])

Options to use for tunneled TLS connections.

tls_handshake_timeout (infinity)

Handshake timeout for tunneled TLS connections.


http_opts() :: #{
    keepalive             => timeout(),
    transform_header_name => fun((binary()) -> binary()),
    version               => 'HTTP/1.1' | 'HTTP/1.0'

Configuration for the HTTP protocol.

The default value is given next to the option name:

keepalive (5000)

Time between pings in milliseconds. Since the HTTP protocol has no standardized way to ping the server, Gun will simply send an empty line when the connection is idle. Gun only makes a best effort here as servers usually have configurable limits to drop idle connections. Use infinity to disable.

transform_header_name - see below

A function that will be applied to all header names before they are sent to the server. Gun assumes that all header names are in lower case. This function is useful if you, for example, need to re-case header names in the event that the server incorrectly considers the case of header names to be significant.

version ('HTTP/1.1')

HTTP version to use.


http2_opts() :: #{
    keepalive => timeout()

Configuration for the HTTP/2 protocol.

The default value is given next to the option name:

keepalive (5000)

Time between pings in milliseconds.


opts() :: #{
    connect_timeout => timeout(),
    http_opts       => http_opts(),
    http2_opts      => http2_opts(),
    protocols       => [http | http2],
    retry           => non_neg_integer(),
    retry_timeout   => pos_integer(),
    trace           => boolean(),
    transport       => tcp | tls,
    transport_opts  => [gen_tcp:connect_option()] | [ssl:connect_option()],
    ws_opts         => ws_opts()

Configuration for the connection.

The default value is given next to the option name:

connect_timeout (infinity)

Connection timeout.

http_opts (#{})

Options specific to the HTTP protocol.

http2_opts (#{})

Options specific to the HTTP/2 protocol.

protocols - see below

Ordered list of preferred protocols. When the transport is tcp, this list must contain exactly one protocol. When the transport is tls, this list must contain at least one protocol and will be used to negotiate a protocol via ALPN. When the server does not support ALPN then http will always be used. Defaults to [http] when the transport is tcp, and [http2, http] when the transport is tls.

retry (5)

Number of times Gun will try to reconnect on failure before giving up.

retry_timeout (5000)

Time between retries in milliseconds.

trace (false)

Whether to enable dbg tracing of the connection process. Should only be used during debugging.

transport - see below

Whether to use TLS or plain TCP. The default varies depending on the port used. Port 443 defaults to tls. All other ports default to tcp.

transport_opts ([])

Transport options. They are TCP options or TLS options depending on the selected transport.

ws_opts (#{})

Options specific to the Websocket protocol.


req_opts() :: #{
    reply_to => pid()

Configuration for a particular request.

The default value is given next to the option name:

reply_to (self())

The pid of the process that will receive the response messages.


ws_opts() :: #{
    compress  => boolean(),
    protocols => [{binary(), module()}]

Configuration for the Websocket protocol.

The default value is given next to the option name:

compress (false)

Whether to enable permessage-deflate compression. This does not guarantee that compression will be used as it is the server that ultimately decides. Defaults to false.

protocols ([])

A non-empty list enables Websocket protocol negotiation. The list of protocols will be sent in the sec-websocket-protocol request header. The handler module interface is currently undocumented and must be set to gun_ws_h.


  • 1.3: Add the CONNECT destination's protocols option and deprecate the previously introduced protocol option.
  • 1.2: Introduce the type connect_destination().

See also


Gun 1.3 Function Reference


Version select

Like my work? Donate!

Donate to Loïc Hoguin because his work on Cowboy, Ranch, Gun and is fantastic:

Recurring payment options are also available via GitHub Sponsors. These funds are used to cover the recurring expenses like food, dedicated servers or domain names.