hackney (hackney v4.2.3)
View SourceSummary
Functions
Read the full response body after start_response/1. Consumes the response stream and returns it as a single binary.
Same as body/1 with a receive timeout.
Close a connection.
Connect to a host and return a connection handle (hackney_conn PID).
Parse cookies from response headers.
Finish sending the streaming request body.
Get the final URL after following redirects. First checks the stored location (set after redirects), then falls back to the Location header from the last response.
Parse a proxy URL and extract host, port, and optional credentials. Supports URLs like: - "http://proxy.example.com:8080" - "http://user:pass@proxy.example.com:8080" - "https://admin:secret@secure-proxy.example.com:443" - "socks5://socks.example.com:1080" - "socks5://user:pass@socks.example.com:1080"
Pause async streaming.
Get the peer SSL certificate. Returns the DER-encoded certificate of the peer, or an error if the connection is not SSL or the certificate is unavailable.
Get the remote address and port.
Get redirect location from headers.
Make a request.
Make a request.
Resume async streaming.
Send a chunk of the request body. Used when request was initiated with body = stream.
Send a request on an existing connection.
Set socket options.
Get the local address and port.
Start receiving the response after sending the full body. Returns {ok, Status, Headers, ConnPid}.
Stop async mode and return to sync mode.
Read the response body one chunk at a time after start_response/1. Returns {ok, Chunk} per chunk and done when the body is fully consumed.
Request next chunk in {async, once} mode.
Close WebSocket connection gracefully.
Connect to a WebSocket server. URL should use ws:// or wss:// scheme.
Receive a WebSocket frame (passive mode only). Blocks until a frame is received or timeout. Returns {ok, Frame} or {error, Reason}.
Send a WebSocket frame. Frame types: - {text, Data} - Text message - {binary, Data} - Binary message - ping | {ping, Data} - Ping frame - pong | {pong, Data} - Pong frame - close | {close, Code, Reason} - Close frame
Set WebSocket options. Supported options: [{active, true | false | once}]
Close a WebTransport session gracefully.
Close a stream gracefully (send FIN).
Connect to a WebTransport server.
Open a new stream multiplexed over the session.
Receive the next message on the default channel (passive mode).
Abruptly terminate a stream with an error code.
Send on a WebTransport connection. Frame forms: {text, Data}, {binary, Data}, Data (write to the default stream); {datagram, Data}; {stream, StreamId, Data} or {stream, StreamId, Data, fin|nofin}.
Send an unreliable datagram.
Return WebTransport session information.
Set WebTransport options. Supported: [{active, true | false | once}]
Ask the peer to stop sending on a stream.
Receive the next chunk on a stream (passive mode).
Write to a stream (no FIN).
Write to a stream, optionally closing the write side (FIN).
Types
-type conn() :: pid().
-type url() :: #hackney_url{transport :: atom(), scheme :: atom() | binary(), netloc :: binary(), raw_path :: binary() | undefined, path :: binary() | undefined | nil, qs :: binary(), fragment :: binary(), host :: string(), port :: integer() | undefined, user :: binary(), password :: binary()} | binary().
Functions
Read the full response body after start_response/1. Consumes the response stream and returns it as a single binary.
Same as body/1 with a receive timeout.
-spec close(conn()) -> ok.
Close a connection.
-spec connect(module(), string(), inet:port_number()) -> {ok, conn()} | {error, term()}.
Connect to a host and return a connection handle (hackney_conn PID).
Parse cookies from response headers.
Finish sending the streaming request body.
Get the final URL after following redirects. First checks the stored location (set after redirects), then falls back to the Location header from the last response.
-spec parse_proxy_url(binary() | string()) -> {ok, #{scheme := atom(), host := string(), port := inet:port_number(), user := binary() | undefined, password := binary() | undefined}} | {error, invalid_proxy_url}.
Parse a proxy URL and extract host, port, and optional credentials. Supports URLs like: - "http://proxy.example.com:8080" - "http://user:pass@proxy.example.com:8080" - "https://admin:secret@secure-proxy.example.com:443" - "socks5://socks.example.com:1080" - "socks5://user:pass@socks.example.com:1080"
Returns a map with keys: scheme, host, port, user, password Fixes issue #741: Extract proxy basic auth from URL
-spec pause_stream(conn()) -> ok.
Pause async streaming.
Get the peer SSL certificate. Returns the DER-encoded certificate of the peer, or an error if the connection is not SSL or the certificate is unavailable.
-spec peername(conn()) -> {ok, {inet:ip_address(), inet:port_number()}} | {error, term()}.
Get the remote address and port.
Get redirect location from headers.
-spec request(url()) -> request_ret().
Make a request.
-spec request(atom() | binary(), url()) -> request_ret().
-spec request(atom() | binary(), url(), list()) -> request_ret().
Make a request.
Args: - Method: HTTP method (get, post, put, delete, etc.) - URL: Full URL or parsed hackney_url record - Headers: List of headers - Body: Request body (binary, iolist, {form, KVs}, {file, Path}, etc.) - Options: Request options
Options: - async: true | once - Receive response asynchronously - stream_to: PID to receive async messages - follow_redirect: Follow redirects automatically - max_redirect: Maximum number of redirects (default 5) - location_trusted: If true, forward auth credentials on cross-host redirects (default false) - pool: Pool name or false for no pooling - connect_timeout: Connection timeout in ms (default 8000) - recv_timeout: Receive timeout in ms (default 5000)
Returns: - {ok, Status, Headers, Body}: Success with response body - {ok, Status, Headers}: HEAD request - {ok, Ref}: Async mode - use stream_next/1 to receive messages - {ok, ConnPid}: Streaming body mode (body = stream) - use send_body/2, finish_send_body/1 - {error, Reason}: Error
Note: The with_body option is deprecated and ignored. Body is always returned directly.
-spec resume_stream(conn()) -> ok.
Resume async streaming.
Send a chunk of the request body. Used when request was initiated with body = stream.
-spec send_request(conn(), {atom(), binary(), list(), term()}) -> {ok, integer(), list(), conn()} | {ok, integer(), list()} | {error, term()}.
Send a request on an existing connection.
Set socket options.
-spec sockname(conn()) -> {ok, {inet:ip_address(), inet:port_number()}} | {error, term()}.
Get the local address and port.
Start receiving the response after sending the full body. Returns {ok, Status, Headers, ConnPid}.
Stop async mode and return to sync mode.
Read the response body one chunk at a time after start_response/1. Returns {ok, Chunk} per chunk and done when the body is fully consumed.
-spec stream_next(conn()) -> ok.
Request next chunk in {async, once} mode.
-spec ws_close(pid()) -> ok.
Close WebSocket connection gracefully.
Connect to a WebSocket server. URL should use ws:// or wss:// scheme.
Options:
- active: false | true | once (default false)
- headers: Extra headers for upgrade request
- protocols: Sec-WebSocket-Protocol values
- connect_timeout: Connection timeout in ms (default 8000)
- recv_timeout: Receive timeout in ms (default infinity)
- connect_options: Options passed to transport connect
- ssl_options: Additional SSL options
Returns {ok, WsPid} on success, where WsPid is the hackney_ws process.
-spec ws_recv(pid()) -> {ok, hackney_ws:ws_frame()} | {error, term()}.
Receive a WebSocket frame (passive mode only). Blocks until a frame is received or timeout. Returns {ok, Frame} or {error, Reason}.
-spec ws_recv(pid(), timeout()) -> {ok, hackney_ws:ws_frame()} | {error, term()}.
-spec ws_send(pid(), hackney_ws:ws_frame()) -> ok | {error, term()}.
Send a WebSocket frame. Frame types: - {text, Data} - Text message - {binary, Data} - Binary message - ping | {ping, Data} - Ping frame - pong | {pong, Data} - Pong frame - close | {close, Code, Reason} - Close frame
Set WebSocket options. Supported options: [{active, true | false | once}]
-spec wt_close(pid()) -> ok.
Close a WebTransport session gracefully.
-spec wt_close(pid(), {non_neg_integer(), binary()}) -> ok.
-spec wt_close_stream(pid(), non_neg_integer()) -> ok | {error, term()}.
Close a stream gracefully (send FIN).
Connect to a WebTransport server.
The URL must use the https:// scheme (wss:// is accepted as an alias so existing ws_* code can switch over by changing only the function name). WebTransport always runs over TLS.
Options:
- transport: h3 (default) or h2
- active: false | true | once (default false)
- headers: extra headers for the CONNECT request
- connect_timeout: connection timeout in ms (default 8000)
- recv_timeout: default receive timeout in ms (default infinity)
- ssl_options: TLS options (verify, cacerts/cacertfile, cert/certfile, key/keyfile)
- verify: verify_peer (default) | verify_none
- compat_mode: latest (default) | legacy_browser_compat
- max_recv_buffer: passive buffer cap in bytes (default 64 MiB)
Returns {ok, WtPid} on success, where WtPid is the hackney_wt process.
-spec wt_open_stream(pid(), bidi | uni) -> {ok, non_neg_integer()} | {error, term()}.
Open a new stream multiplexed over the session.
-spec wt_recv(pid()) -> {ok, hackney_wt:wt_msg()} | {error, term()}.
Receive the next message on the default channel (passive mode).
-spec wt_recv(pid(), timeout()) -> {ok, hackney_wt:wt_msg()} | {error, term()}.
-spec wt_reset_stream(pid(), non_neg_integer(), non_neg_integer()) -> ok | {error, term()}.
Abruptly terminate a stream with an error code.
-spec wt_send(pid(), hackney_wt:wt_frame()) -> ok | {error, term()}.
Send on a WebTransport connection. Frame forms: {text, Data}, {binary, Data}, Data (write to the default stream); {datagram, Data}; {stream, StreamId, Data} or {stream, StreamId, Data, fin|nofin}.
Send an unreliable datagram.
Return WebTransport session information.
Set WebTransport options. Supported: [{active, true | false | once}]
-spec wt_stop_sending(pid(), non_neg_integer(), non_neg_integer()) -> ok | {error, term()}.
Ask the peer to stop sending on a stream.
-spec wt_stream_recv(pid(), non_neg_integer()) -> hackney_wt:stream_msg().
Receive the next chunk on a stream (passive mode).
-spec wt_stream_recv(pid(), non_neg_integer(), timeout()) -> hackney_wt:stream_msg().
-spec wt_stream_send(pid(), non_neg_integer(), iodata()) -> ok | {error, term()}.
Write to a stream (no FIN).
-spec wt_stream_send(pid(), non_neg_integer(), iodata(), fin | nofin) -> ok | {error, term()}.
Write to a stream, optionally closing the write side (FIN).