March Docs

HttpClient

HttpClient: high-level composable HTTP client with step pipeline.

This is Layer 3 of March's HTTP library. It provides a Req-style three-phase pipeline (request steps, response steps, error steps) built on top of HttpTransport.

Usage: let client = HttpClient.new_client() |> HttpClient.add_request_step("auth", HttpClient.Steps.bearer_auth("my-token")) |> HttpClient.with_redirects(5) let resp = HttpClient.get(client, "http://api.example.com/users")

Types

typeTransportErrorTransportError =#
typeHttpErrorHttpError =#
typeRequestStepEntryRequestStepEntry = RequestStepEntry(String, a)#
typeResponseStepEntryResponseStepEntry = ResponseStepEntry(String, a)#
typeErrorRecoveryErrorRecovery = Recover(a) | Fail(HttpError)#
typeErrorStepEntryErrorStepEntry = ErrorStepEntry(String, a)#
typeClientClient = Client(#

Functions

fnadd_error_stepadd_error_step(client, name, step)#
fnadd_request_stepadd_request_step(client, name, step)#
fnadd_response_stepadd_response_step(client, name, step)#
fnappend_to_listappend_to_list(xs, x)#
fndeletedelete(client, url)#
fngetget(client, url)#
fnhandle_redirectshandle_redirects(req, resp, max, count)#
fnhc_effective_porthc_effective_port(sch, port_opt)#
fnhc_is_sensitive_header_namehc_is_sensitive_header_name(name)#
fnhc_same_originhc_same_origin(req_a, req_b)#
fnhc_strip_sensitive_headershc_strip_sensitive_headers(headers)#
fnlist_stepslist_steps(client)#
fnnew_clientnew_client()#

Create a new bare client with no steps and no redirect/retry.

fnpostpost(client, url, bdy)#
fnput_requestput_request(client, url, bdy)#
fnreconnect_and_retryreconnect_and_retry(req, retries_left)#
fnrunrun(client, req)#
fnrun_error_stepsrun_error_steps(steps, req, err)#
fnrun_on_fdrun_on_fd(fd, req_steps, resp_steps, err_steps, max_redir, max_retries, req)#

Run a request through the full step pipeline.

  1. Run request steps left-to-right
  2. Send via HttpTransport.request (with retry if configured)
  3. Handle redirects if configured
  4. Run response steps left-to-right
  5. On error, run error steps (first Recover wins)
fnrun_request_stepsrun_request_steps(steps, req)#
fnrun_response_stepsrun_response_steps(steps, req, resp)#
fnstep_base_urlstep_base_url(base)#

Returns a request step that sets a base URL on requests.

fnstep_basic_authstep_basic_auth(user, pass)#

Returns a request step that adds HTTP Basic authorization header.

fnstep_bearer_authstep_bearer_auth(token)#

Returns a request step that adds a Bearer token authorization header.

fnstep_content_typestep_content_type(ct)#

Returns a request step that sets the Content-Type header.

fnstep_default_headersstep_default_headers(req)#

Request step: add default User-Agent and Accept headers.

fnstep_raise_on_errorstep_raise_on_error(req, resp)#

Response step: return Err for 4xx/5xx status codes.

fnstream_getstream_get(client, url, on_chunk)#

Stream a GET response body. Calls on_chunk(chunk_string) for each chunk as it arrives over the wire. Returns Ok((status, headers, last_result)) where last_result is the return value of the final on_chunk call.

Example:
  HttpClient.stream_get(client, "http://example.com/bigfile", fn(chunk) ->
    print("got " ++ int_to_string(string_length(chunk)) ++ " bytes")
  end)
fntransport_keepalivetransport_keepalive(fd, req, retries_left)#
fntransport_with_retrytransport_with_retry(req, retries_left)#
fnwith_connectionwith_connection(client, url, callback)#

Open a keep-alive connection and run a function with it. The function receives a do_request callback that sends requests on the shared connection. Connection is closed when done.

Example:
  HttpClient.with_connection(client, "http://api.example.com", fn(do_request) -> do
    let r1 = do_request(Http.get_request("/users"))
    let r2 = do_request(Http.get_request("/posts"))
    (r1, r2)
  end
  end)
fnwith_redirectswith_redirects(client, max)#
fnwith_retrywith_retry(client, max_attempts, backoff_ms)#