curlify
Utilitys for generating curl commands. Use the builder api, convert a gleam_http request or
parse a curl command line string.
Types
Request body variants. Text renders as --data, Json renders as
--json, and Form renders as one or more --data-urlencode.
pub type Body {
Empty
Text(String)
Json(String)
Form(List(#(String, String)))
}
Constructors
-
Empty -
Text(String) -
Json(String) -
Form(List(#(String, String)))
Accumulated state for rendering a curl command. HTTP-level fields plus common curl options.
pub type Curl {
Curl(
method: http.Method,
url: String,
headers: List(#(String, String)),
body: Body,
follow_redirects: Bool,
verbose: Bool,
insecure: Bool,
compressed: Bool,
timeout: Int,
basic_auth: option.Option(#(String, String)),
)
}
Constructors
-
Curl( method: http.Method, url: String, headers: List(#(String, String)), body: Body, follow_redirects: Bool, verbose: Bool, insecure: Bool, compressed: Bool, timeout: Int, basic_auth: option.Option(#(String, String)), )
Errors that can occur when working with curl strings or requests.
pub type CurlParseError {
MissingUrl
RequestBuildError
UnknownHttpMethod(method: String)
BadTimeoutValue
}
Constructors
-
MissingUrlThe curl command had no URL positional argument.
-
RequestBuildErrorThe URL parsed from the curl command could not be used to construct a valid Request (e.g. malformed scheme).
-
UnknownHttpMethod(method: String)The curl command used an HTTP method that is not recognized by
gleam/http(e.g.-XWHATEVER). -
BadTimeoutValueThe curl command used an invalid timeout value (not an integer).
Values
pub fn curl_to_request(
input: String,
) -> Result(request.Request(String), CurlParseError)
Parse a curl command string and convert the result directly into a gleam_http Request.
Equivalent to parse(input) |> result.try(to_request(?)).
Returns Error(RequestBuildError) if the parsed URL cannot be
used to construct a Request.
pub fn from_request(req: request.Request(String)) -> Curl
Build a Curl from a gleam_http Request.
If the request has an Authorization: Basic header, it is decoded
and stored in basic_auth, and the header is removed from the list
to avoid redundancy when rendering the curl command.
If the request has a Content-Type: application/json header, the body
is stored as Json(...) so it renders as --json.
pub fn from_url(url: String) -> Curl
Create a Curl from a URL string. The URL is stored as-is and
validated later if to_request is called.
pub fn parse(input: String) -> Result(Curl, CurlParseError)
Parse a curl command string into a Curl struct.
Supported flags:
- -X/–request
- -H/–header
- -d/–data
- –data-raw
- –data-binary
- –json
- –data-urlencode
- -L/–location
- -v/–verbose
- -k/–insecure
- –compressed
- –max-time
- -u/–user
- -A/–user-agent.
Unsupported flags are silently dropped.
Returns Error(BadTimeoutValue) if --max-time is not a valid integer.
The input string is first tokenized using POSIX shell quoting
rules (single quotes, double quotes, backslash), then parsed
directly into a Curl struct. Returns an error if the URL
is missing.
pub fn request_to_curl(req: request.Request(String)) -> String
Convert a gleam_http request directly into a curl command string.
Equivalent to from_request(req) |> to_string.
pub fn set_basic_auth(
curl: Curl,
username: String,
password: String,
) -> Curl
Set HTTP basic authentication credentials (-u username:password).
pub fn set_header(curl: Curl, key: String, value: String) -> Curl
Set a header on a Curl. Replaces any existing header with the same key.
pub fn set_method(curl: Curl, method: http.Method) -> Curl
Set the HTTP method on a Curl.
pub fn set_timeout(curl: Curl, seconds: Int) -> Curl
Set a maximum transfer time in seconds (--max-time).
pub fn set_user_agent(curl: Curl, agent: String) -> Curl
Set a custom User-Agent header (-A / --user-agent).
pub fn to_args(curl: Curl) -> List(String)
Return the curl arguments as a raw list of strings, without shell escaping. Each element is a single argument suitable for passing to exec or a subprocess.
Example:
to_args(curl) → ["-X", "POST", "--json", "{\"name\": \"test\"}", ...]
to_string(curl) → "curl -X POST --json '{\"name\": \"test\"}' ..."
pub fn to_gleam(curl: Curl) -> Result(String, Nil)
Generate Gleam source code that reconstructs this Curl as a
gleam_http request. The output can be pasted into the body of a
Gleam function.
No import are generated.
The usefulness of this function is debatable.. but here it is anyways.
pub fn to_pretty_string(curl: Curl) -> String
Render a multi-line curl command with backslash continuation. Each flag goes on its own line for readability.
Example:
curl -X POST \
-H 'authorization: Bearer tok' \
--json '{"key": "val"}' \
'https://example.com'
pub fn to_request(
curl: Curl,
) -> Result(request.Request(String), Nil)
The inverse of from_request. Reconstructs a gleam/http/request.Request
from the HTTP-relevant fields of a Curl. If the Curl has basic_auth
set, an Authorization: Basic header is added. If the body is
Json(...), a Content-Type: application/json header is set.