hackney (hackney v1.25.0)

View Source

Summary

Functions

Return the full body sent with the response.

Return the full body sent with the response as long as the body length doesn't go over MaxLength.

Extract raw information from the client context This feature can be useful when you want to create a simple proxy, rerouting on the headers and the status line and continue to forward the connection for example.

close the client

connect a socket and create a client state.

Assign a new controlling process Pid to Client.

return the requested location

pause a response stream, the stream process will hibernate and be woken later by the resume function

peername of the client

make a request

make a request

get request info

resume a paused response stream, the stream process will be awoken

send the request body until eob. It's issued after sending a request using the request and send_request functions.

send a multipart body until eof Possible value are

send a request using the current client state

send a request using the current client state and pass new options to it.

set client options. Options are

skip the full body. (read all the body if needed).

Stream the response body.

sockname of the client

start a response. Useful if you stream the body by yourself. It will fetch the status and headers of the response. and return

stop to receive asynchronously.

Stream the response body.

Stream the response body.

continue to the next stream message. Only use it when {async, once} is set in the client options.

Types

client/0

-type client() ::
          #client{start_time :: term(),
                  mod_metrics :: term(),
                  transport :: term(),
                  host :: term(),
                  port :: term(),
                  netloc :: term(),
                  options :: term(),
                  socket :: term(),
                  socket_ref :: term(),
                  request_ref :: term(),
                  dynamic :: term(),
                  pool_handler :: term(),
                  recv_timeout :: term(),
                  follow_redirect :: term(),
                  max_redirect :: term(),
                  force_redirect :: term(),
                  retries :: term(),
                  redirect :: term(),
                  location :: term(),
                  parser :: term(),
                  headers :: term(),
                  state :: term(),
                  response_state :: term(),
                  mp_boundary :: term(),
                  req_type :: term(),
                  expect :: term(),
                  async :: term(),
                  with_body :: term(),
                  max_body :: term(),
                  stream_to :: term(),
                  send_fun :: term(),
                  body_state :: term(),
                  multipart :: term(),
                  req_chunk_size :: term(),
                  buffer :: term(),
                  partial_headers :: term(),
                  version :: term(),
                  clen :: term(),
                  te :: term(),
                  connection :: term(),
                  method :: term(),
                  path :: term(),
                  ctype :: term()}.

client_ref/0

-type client_ref() :: 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(Ref)

-spec body(client_ref()) -> {ok, binary()} | {error, atom()} | {error, {closed, binary()}}.

Return the full body sent with the response.

body(Ref, MaxLength)

-spec body(client_ref(), non_neg_integer() | infinity) ->
              {ok, binary()} | {error, atom()} | {error, {closed, binary()}}.

Return the full body sent with the response as long as the body length doesn't go over MaxLength.

cancel_request(Ref)

-spec cancel_request(client_ref()) ->
                        {ok, {atom(), inet:socket(), binary(), hackney_response:response_state()}} |
                        {error, term()}.

Extract raw information from the client context This feature can be useful when you want to create a simple proxy, rerouting on the headers and the status line and continue to forward the connection for example.

return: {ResponseState, Transport, Socket, Buffer} | {error, Reason}

  • Response: waiting_response, on_status, on_headers, on_body
  • Transport: The current transport module
  • Socket: the current socket
  • Buffer: Data fetched but not yet processed

checkout(URL)

checkout(URL, Headers)

checkout(URL, Headers, Body)

checkout(URL, Headers, Body, Options)

close(Ref)

close the client

connect(URL)

connect(Hackney_url, Options)

connect(Transport, Host, Port)

connect a socket and create a client state.

connect(Transport, Host, Port, Options)

controlling_process(Ref, Pid)

-spec controlling_process(client_ref(), pid()) -> ok | {error, closed | not_owner | atom()}.

Assign a new controlling process Pid to Client.

cookies(Headers)

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

copy(URL)

copy(URL, Headers)

copy(URL, Headers, Body)

copy(URL, Headers, Body, Options)

delete(URL)

delete(URL, Headers)

delete(URL, Headers, Body)

delete(URL, Headers, Body, Options)

finish_send_body(Ref)

get(URL)

get(URL, Headers)

get(URL, Headers, Body)

get(URL, Headers, Body, Options)

head(URL)

head(URL, Headers)

head(URL, Headers, Body)

head(URL, Headers, Body, Options)

location(Ref)

-spec location(client_ref()) -> binary().

return the requested location

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)

patch(URL)

patch(URL, Headers)

patch(URL, Headers, Body)

patch(URL, Headers, Body, Options)

pause_stream(Ref)

-spec pause_stream(client_ref()) -> ok | {error, req_not_found}.

pause a response stream, the stream process will hibernate and be woken later by the resume function

peername(Ref)

peername of the client

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)

report(URL)

report(URL, Headers)

report(URL, Headers, Body)

report(URL, Headers, Body, Options)

request(URL)

-spec request(url() | binary() | list()) ->
                 {ok, integer(), list(), client_ref()} | {ok, integer(), list()} | {error, term()}.

make a request

request(Method, URL)

-spec request(term(), url() | binary() | list()) ->
                 {ok, integer(), list(), client_ref()} | {ok, integer(), list()} | {error, term()}.

make a request

request(Method, URL, Headers)

-spec request(term(), url() | binary() | list(), list()) ->
                 {ok, integer(), list(), client_ref()} | {ok, integer(), list()} | {error, term()}.

make a request

request(Method, URL, Headers, Body)

-spec request(term(), url() | binary() | list(), list(), term()) ->
                 {ok, integer(), list(), client_ref()} | {ok, integer(), list()} | {error, term()}.

make a request

request(Method, Hackney_url, Headers0, Body, Options0)

-spec request(term(), url() | binary() | list(), list(), term(), list()) ->
                 {ok, integer(), list(), client_ref()} |
                 {ok, integer(), list(), binary()} |
                 {ok, integer(), list()} |
                 {ok, client_ref()} |
                 {error, term()}.

make a request

Args:

  • Method>: method used for the request (get, post, ...)
  • Url: full url of the request
  • Headers Proplists
  • Body:
    • {form, [{K, V}, ...]}: send a form url encoded
    • {multipart, [{K, V}, ...]}: send a form using multipart
    • {file, "/path/to/file"}: to send a file
    • Bin: binary or iolist
  • Options:[{connect_options, connect_options(), {ssl_options, ssl_options()}, Others]
  • connect_options(): The default connect_options are [binary, {active, false}, {packet, raw}]). For valid options see the gen_tcp options.
  • ssl_options(): See the ssl options from the ssl module.
  • with_body: when this option is passed the body is returned directly. The response is {ok, Status, Headers, Body}
  • max_body: sets maximum allowed size of the body if with_body is true
  • async: receive the response asynchronously The function return {ok, StreamRef}. When {async, once} is used the response will be received only once. To receive the other messages use the function hackney:stream_next/1
  • {path_encode_fun, fun()}: function used to encode the path. if not set it will use hackney_url:pathencode/1 the function takes the binary path as entry and return a new encoded path.
  • {stream_to, pid()}: If async is true or once, the response messages will be sent to this PID.
  • {cookie, list() | binary()} : to set a cookie or a list of cookies.
  • Others options are:
    • {follow_redirect, boolean}: false by default, follow a redirection
    • {max_redirect, integer}: 5 by default, the maximum of redirection for a request
    • {force_redirect, boolean}: false by default, to force the redirection even on POST
    • {basic_auth, {binary, binary}}`: HTTP basic auth username and password. Only allowed over HTTPS unless {insecure_basic_auth, true} is also set.</li> <li>`{insecure_basic_auth, boolean}: true by default. When true, allows basic auth over unencrypted HTTP connections (security risk). Can also be set globally via application:set_env(hackney, insecure_basic_auth, false).
    • {proxy, proxy_options()}: to connect via a proxy.
    • insecure: to perform "insecure" SSL connections and transfers without checking the certificate
    • {checkout_timeout, infinity | integer()}: timeout used when checking out a socket from the pool, in milliseconds. By default is equal to connect_timeout
    • {connect_timeout, infinity | integer()}: timeout used when establishing a connection, in milliseconds. Default is 8000
    • {recv_timeout, infinity | integer()}: timeout used when receiving data over a connection. Default is 5000
    Note: if the response is async, only follow_redirect is take in consideration for the redirection. If a valid redirection happen you receive the messages:
    • {redirect, To, Headers}
    • {see_other, To, Headers} for status 303 POST requests.
  • proxy_options(): options to connect by a proxy:

    • binary(): url to use for the proxy. Used for basic HTTP proxy
    • {Host::binary(), Port::binary}: Host and port to connect, for HTTP proxy
    • {socks5, Host::binary(), Port::binary()}: Host and Port to connect to a socks5 proxy.
    • {connect, Host::binary(), Port::binary()}: Host and Port to connect to an HTTP tunnel.

Note: instead of doing hackney:request(Method, ...) you can also do hackney:Method(...) if you prefer to use the REST syntax.

Return:

  • {ok, ResponseStatus, ResponseHeaders}: On HEAD request if the response succeeded.
  • {ok, ResponseStatus, ResponseHeaders, Ref}: When the response succeeded. The request reference is used later to retrieve the body.
  • {ok, ResponseStatus, ResponseHeaders, Body}: When the option with_body is set to true and the response succeeded.
  • {ok, Ref} Return the request reference when you decide to stream the request. You can use the returned reference to stream the request body and continue to handle the response.
  • {error, {closed, PartialBody}} A body was expected but instead the remote closed the response after sending the headers. Equivalent to the curl message no chunk, no close, no size. Assume close to signal end.
  • {error, term()} other errors.

request_info(Ref)

-spec request_info(client_ref()) -> list().

get request info

resume_stream(Ref)

-spec resume_stream(client_ref()) -> ok | {error, req_not_found}.

resume a paused response stream, the stream process will be awoken

search(URL)

search(URL, Headers)

search(URL, Headers, Body)

search(URL, Headers, Body, Options)

send_body(Ref, Body)

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

send the request body until eob. It's issued after sending a request using the request and send_request functions.

send_multipart_body(Ref, Body)

-spec send_multipart_body(client_ref(), term()) -> ok | {error, term()}.

send a multipart body until eof Possible value are :

  • eof: end the multipart request
  • {file, Path}: to stream a file
  • {file, Path, ExtraHeaders}: to stream a file
  • {data, Name, Content}: to send a full part
  • {data, Name, Content, ExtraHeaders}: to send a full part
  • {part, Name, Len}: to start sending a part with a known length in a streaming fashion
  • {part, Name, Len, ExtraHeader}: to start sending a part in a streaming fashion
  • {part, Name}: to start sending a part without length in a streaming fashion
  • {part, Name, ExtraHeader}: to start sending a part without length in a streaming fashion
  • {part_bin, Bin}: To send part of part
  • {part, eof}: To notify the end of the 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

Note: You can calculate the full length of a multipart stream using the function hackney_multipart:len_mp_stream/2 .

send_request(Ref, Req)

send a request using the current client state

send_request(Ref, Req, Options)

send a request using the current client state and pass new options to it.

setopts(Ref, Options)

-spec setopts(client_ref(), list()) -> ok.

set client options. Options are:

  • async: to fetch the response asynchronously
  • {async, once}: to receive the response asynchronously one time. To receive the next message use the function hackney:stream_next/1.
  • {stream_to, pid()}: to set the pid where the messages of an asynchronous response will be sent.
  • {follow_redirect, bool()} : if true a redirection will be followed when the response is received synchronously
  • {force_redirect, bool()} : if true a 301/302 redirection will be followed even on POST.
  • {max_redirect, integer()} the maximum number of redirections that will be followed

skip_body(Ref)

-spec skip_body(client_ref()) -> ok | {error, atom()}.

skip the full body. (read all the body if needed).

skip_multipart(Ref)

-spec skip_multipart(client_ref()) -> ok | {error, term()}.

Stream the response body.

sockname(Ref)

sockname of the client

start_response(Ref)

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

start a response. Useful if you stream the body by yourself. It will fetch the status and headers of the response. and return

stop_async(Ref)

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

stop to receive asynchronously.

stream_body(Ref)

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

Stream the response body.

stream_multipart(Ref)

-spec stream_multipart(client_ref()) ->
                          {headers, list()} | {body, binary()} | eof | end_of_part | {error, term()}.

Stream the response body.

Return:

  • {headers, Headers}: the part headers
  • {body, Bin}: part of the content
  • end_of_part : end of part
  • mp_mixed: notify the beginning of a mixed multipart part
  • mp_mixed_eof: notify the end of a mixed multipart part
  • eof: notify the end of the multipart request

stream_next(Ref)

-spec stream_next(client_ref()) -> ok | {error, req_not_found}.

continue to the next stream message. Only use it when {async, once} is set in the client options.

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)