HTTP.FetchOptions (http_fetch v0.8.0)
Options processing and validation for HTTP.fetch/2 requests.
This module handles the conversion of various option formats (maps, keyword lists,
structs) into structured options for :httpc.request/4. It provides a flexible
API that supports both simple and advanced HTTP client configurations.
Options Categories
Options are divided into two main categories:
HTTP Options (3rd argument to
:httpc.request/4) - Request-specific settings:timeout- Request timeout in millisecondsconnect_timeout- Connection timeout in millisecondsssl- SSL/TLS optionsautoredirect- Follow redirects automaticallyproxy_auth- Proxy authenticationversion- HTTP versionrelaxed- Relaxed parsing mode
Client Options (4th argument to
:httpc.request/4) - Client behavior settings:sync- Synchronous/asynchronous mode (default: false)stream- Response streaming configurationbody_format- Response body format (:string or :binary)full_result- Return full HTTP responseheaders_as_is- Preserve header casesocket_opts- Socket-level optionsreceiver- Custom receiver processipv6_host_with_brackets- IPv6 host formatting
Basic Usage
# Simple options as keyword list
HTTP.fetch("https://api.example.com", [
method: "POST",
headers: %{"Content-Type" => "application/json"},
timeout: 10_000
])
# Complex options with HTTP and client settings
HTTP.fetch("https://api.example.com", [
method: "GET",
timeout: 5_000,
connect_timeout: 2_000,
ssl: [verify: :verify_peer],
body_format: :binary
])Advanced Configuration
# Using options and opts keywords for fine control
HTTP.fetch("https://api.example.com", [
method: "POST",
body: "data",
# Request-specific options (3rd arg to :httpc.request)
options: [
timeout: 10_000,
connect_timeout: 5_000
],
# Client-specific options (4th arg to :httpc.request)
opts: [
sync: false,
body_format: :binary
]
])Flat vs Structured Options
The module supports both flat and structured option formats:
# Flat format (recommended for simplicity)
[method: "POST", timeout: 5_000, body_format: :binary]
# Structured format (for explicit control)
[
method: "POST",
options: [timeout: 5_000],
opts: [body_format: :binary]
]Both formats are equivalent; the module automatically categorizes options.
Summary
Functions
Extracts body from options.
Extracts content type from options.
Extracts headers from options.
Extracts the HTTP method from options.
Creates a new FetchOptions struct from various input formats. Supports flat map, keyword list, or existing FetchOptions struct.
Converts FetchOptions to HTTP options for :httpc.request 3rd argument. Returns keyword list of HttpOptions.
Converts FetchOptions to options for :httpc.request 4th argument. Returns keyword list of Options.
Types
@type t() :: %HTTP.FetchOptions{ autoredirect: boolean() | nil, body: any(), body_format: atom() | nil, connect_timeout: integer() | nil, content_type: String.t() | nil, full_result: boolean() | nil, headers: HTTP.Headers.t(), headers_as_is: boolean() | nil, ipv6_host_with_brackets: boolean() | nil, method: atom(), options: keyword(), opts: keyword(), proxy_auth: tuple() | nil, receiver: pid() | function() | tuple() | nil, relaxed: boolean() | nil, signal: any() | nil, socket_opts: list() | nil, ssl: list() | nil, stream: atom() | tuple() | nil, timeout: integer() | nil, unix_socket: String.t() | nil, version: String.t() | nil }
Functions
Extracts body from options.
Extracts content type from options.
@spec get_headers(t()) :: HTTP.Headers.t()
Extracts headers from options.
Extracts the HTTP method from options.
Creates a new FetchOptions struct from various input formats. Supports flat map, keyword list, or existing FetchOptions struct.
Converts FetchOptions to HTTP options for :httpc.request 3rd argument. Returns keyword list of HttpOptions.
Converts FetchOptions to options for :httpc.request 4th argument. Returns keyword list of Options.