hackney (hackney v2.0.0)

View Source

Summary

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.

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

conn/0

-type conn() :: pid().

request_ret/0

-type request_ret() ::
          {ok, integer(), list(), conn()} |
          {ok, integer(), list(), binary()} |
          {ok, integer(), list()} |
          {ok, reference()} |
          {ok, conn()} |
          {error, term()}.

url/0

-type url() ::
          #hackney_url{transport :: atom(),
                       scheme :: atom(),
                       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

body(ConnPid)

-spec body(conn()) -> {ok, binary()} | {error, term()}.

Get the full response body. After reading the body, the connection is automatically released back to the pool.

body(ConnPid, Timeout)

-spec body(conn(), timeout()) -> {ok, binary()} | {error, term()}.

checkout(URL)

checkout(URL, Headers)

checkout(URL, Headers, Body)

checkout(URL, Headers, Body, Options)

close(ConnPid)

-spec close(conn()) -> ok.

Close a connection.

connect(URL)

connect(Hackney_url, Options)

connect(Transport, Host, Port)

-spec connect(module(), string(), inet:port_number()) -> {ok, conn()} | {error, term()}.

Connect to a host and return a connection handle (hackney_conn PID).

connect(Transport, Host, Port, Options)

-spec connect(module(), string(), inet:port_number(), list()) -> {ok, conn()} | {error, term()}.

cookies(Headers)

-spec cookies(list()) -> list().

Parse cookies from response headers.

copy(URL)

copy(URL, Headers)

copy(URL, Headers, Body)

copy(URL, Headers, Body, Options)

default_ua()

delete(URL)

delete(URL, Headers)

delete(URL, Headers, Body)

delete(URL, Headers, Body, Options)

finish_send_body(ConnPid)

-spec finish_send_body(conn()) -> ok | {error, term()}.

Finish sending the streaming request body.

get(URL)

get(URL, Headers)

get(URL, Headers, Body)

get(URL, Headers, Body, Options)

get_version()

head(URL)

head(URL, Headers)

head(URL, Headers, Body)

head(URL, Headers, Body, Options)

location(ConnPid)

-spec location(conn()) -> binary() | undefined.

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.

lock(URL)

lock(URL, Headers)

lock(URL, Headers, Body)

lock(URL, Headers, Body, Options)

merge(URL)

merge(URL, Headers)

merge(URL, Headers, Body)

merge(URL, Headers, Body, Options)

mkactivity(URL)

mkactivity(URL, Headers)

mkactivity(URL, Headers, Body)

mkactivity(URL, Headers, Body, Options)

mkcol(URL)

mkcol(URL, Headers)

mkcol(URL, Headers, Body)

mkcol(URL, Headers, Body, Options)

move(URL)

move(URL, Headers)

move(URL, Headers, Body)

move(URL, Headers, Body, Options)

msearch(URL)

msearch(URL, Headers)

msearch(URL, Headers, Body)

msearch(URL, Headers, Body, Options)

notify(URL)

notify(URL, Headers)

notify(URL, Headers, Body)

notify(URL, Headers, Body, Options)

options(URL)

options(URL, Headers)

options(URL, Headers, Body)

options(URL, Headers, Body, Options)

parse_proxy_url(Url)

-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

patch(URL)

patch(URL, Headers)

patch(URL, Headers, Body)

patch(URL, Headers, Body, Options)

pause_stream(ConnPid)

-spec pause_stream(conn()) -> ok.

Pause async streaming.

peercert(ConnPid)

-spec peercert(conn()) -> {ok, binary()} | {error, term()}.

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.

peername(ConnPid)

-spec peername(conn()) -> {ok, {inet:ip_address(), inet:port_number()}} | {error, term()}.

Get the remote address and port.

post(URL)

post(URL, Headers)

post(URL, Headers, Body)

post(URL, Headers, Body, Options)

propfind(URL)

propfind(URL, Headers)

propfind(URL, Headers, Body)

propfind(URL, Headers, Body, Options)

proppatch(URL)

proppatch(URL, Headers)

proppatch(URL, Headers, Body)

proppatch(URL, Headers, Body, Options)

purge(URL)

purge(URL, Headers)

purge(URL, Headers, Body)

purge(URL, Headers, Body, Options)

put(URL)

put(URL, Headers)

put(URL, Headers, Body)

put(URL, Headers, Body, Options)

redirect_location(Headers)

Get redirect location from headers.

report(URL)

report(URL, Headers)

report(URL, Headers, Body)

report(URL, Headers, Body, Options)

request(URL)

-spec request(url()) -> request_ret().

Make a request.

request(Method, URL)

-spec request(atom() | binary(), url()) -> request_ret().

request(Method, URL, Headers)

-spec request(atom() | binary(), url(), list()) -> request_ret().

request(Method, URL, Headers, Body)

-spec request(atom() | binary(), url(), list(), term()) -> request_ret().

request(Method, URL, Headers, Body, Options)

-spec request(atom() | binary(), url(), list(), term(), 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

resume_stream(ConnPid)

-spec resume_stream(conn()) -> ok.

Resume async streaming.

search(URL)

search(URL, Headers)

search(URL, Headers, Body)

search(URL, Headers, Body, Options)

send_body(ConnPid, Data)

-spec send_body(conn(), iodata()) -> ok | {error, term()}.

Send a chunk of the request body. Used when request was initiated with body = stream.

send_request(ConnPid, _)

-spec send_request(conn(), {atom(), binary(), list(), term()}) ->
                      {ok, integer(), list(), conn()} | {ok, integer(), list()} | {error, term()}.

Send a request on an existing connection.

setopts(ConnPid, Options)

-spec setopts(conn(), list()) -> ok | {error, term()}.

Set socket options.

skip_body(ConnPid)

-spec skip_body(conn()) -> ok | {error, term()}.

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.

sockname(ConnPid)

-spec sockname(conn()) -> {ok, {inet:ip_address(), inet:port_number()}} | {error, term()}.

Get the local address and port.

start_response(ConnPid)

-spec start_response(conn()) -> {ok, integer(), list(), conn()} | {error, term()}.

Start receiving the response after sending the full body. Returns {ok, Status, Headers, ConnPid}.

stop_async(ConnPid)

-spec stop_async(conn()) -> ok | {error, term()}.

Stop async mode and return to sync mode.

stream_body(ConnPid)

-spec stream_body(conn()) -> {ok, binary()} | done | {error, term()}.

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.

stream_next(ConnPid)

-spec stream_next(conn()) -> ok.

Request next chunk in {async, once} mode.

subscribe(URL)

subscribe(URL, Headers)

subscribe(URL, Headers, Body)

subscribe(URL, Headers, Body, Options)

trace(URL)

trace(URL, Headers)

trace(URL, Headers, Body)

trace(URL, Headers, Body, Options)

unlock(URL)

unlock(URL, Headers)

unlock(URL, Headers, Body)

unlock(URL, Headers, Body, Options)

unsubscribe(URL)

unsubscribe(URL, Headers)

unsubscribe(URL, Headers, Body)

unsubscribe(URL, Headers, Body, Options)

ws_close(WsPid)

-spec ws_close(pid()) -> ok.

Close WebSocket connection gracefully.

ws_close(WsPid, _)

-spec ws_close(pid(), {integer(), binary()}) -> ok.

ws_connect(URL)

-spec ws_connect(binary() | string()) -> {ok, pid()} | {error, term()}.

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.

ws_connect(URL, Options)

-spec ws_connect(binary() | string(), list()) -> {ok, pid()} | {error, term()}.

ws_recv(WsPid)

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

ws_recv(WsPid, Timeout)

-spec ws_recv(pid(), timeout()) -> {ok, hackney_ws:ws_frame()} | {error, term()}.

ws_send(WsPid, Frame)

-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

ws_setopts(WsPid, Opts)

-spec ws_setopts(pid(), list()) -> ok | {error, term()}.

Set WebSocket options. Supported options: [{active, true | false | once}]