hackney (hackney v2.0.0)
View SourceSummary
Functions
Get the full response body. After reading the body, the connection is automatically released back to the pool.
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.
Skip the response body and close the connection. The connection is closed rather than returned to pool since we can't guarantee the socket state after skipping.
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.
Stream the response body in chunks. Returns {ok, Data} for each chunk, done when complete, or {error, Reason}. When done is returned, the connection is automatically released back to the pool.
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}]
Types
Functions
Get the full response body. After reading the body, the connection is automatically released back to the pool.
-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: - with_body: If true, return full body in response - 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, ConnPid}: Success, use body/1 or stream_body/1 to get body - {ok, Status, Headers, Body}: Success with with_body option - {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
-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.
Skip the response body and close the connection. The connection is closed rather than returned to pool since we can't guarantee the socket state after skipping.
-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.
Stream the response body in chunks. Returns {ok, Data} for each chunk, done when complete, or {error, Reason}. When done is returned, the connection is automatically released back to the pool.
-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}]