var: add a '64dec' function that can base64 decode a string

Verified in test 455 and 487.

If the provided string cannot be base64-decoded, it will instead use
"[64dec-fail]" (without the quotes).

Documented

Ref: #16288
Closes #16330
This commit is contained in:
Daniel Stenberg 2025-02-22 10:49:19 +01:00
parent 54c07fd6c5
commit a6e8ead533
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
6 changed files with 101 additions and 11 deletions

View file

@ -27,10 +27,10 @@ set:
When expanding variables, curl supports a set of functions that can make the
variable contents more convenient to use. It can trim leading and trailing
white space with `trim`, it can output the contents as a JSON quoted string
with `json`, URL encode the string with `url` or base64 encode it with `b64`.
To apply functions to a variable expansion, add them colon separated to the
right side of the variable. Variable content holding null bytes that are not
encoded when expanded cause error.
with `json`, URL encode the string with `url`, base64 encode it with `b64` and
base64 decode it with `64dec`. To apply functions to a variable expansion, add
them colon separated to the right side of the variable. Variable content
holding null bytes that are not encoded when expanded cause error.
Example: get the contents of a file called $HOME/.secret into a variable
called "fix". Make sure that the content is trimmed and percent-encoded when

View file

@ -66,30 +66,45 @@ error.
Available functions:
## trim
## `trim`
removes all leading and trailing white space.
Example:
curl --expand-url https://example.com/{{var:trim}}
## json
## `json`
outputs the content using JSON string quoting rules.
Example:
curl --expand-data {{data:json}} https://example.com
## url
## `url`
shows the content URL (percent) encoded.
Example:
curl --expand-url https://example.com/{{path:url}}
## b64
## `b64`
expands the variable base64 encoded
Example:
curl --expand-url https://example.com/{{var:b64}}
## `64dec`
decodes a base64 encoded character sequence. If the sequence is not possible
to decode, it instead outputs `[64dec-fail]`
Example:
curl --expand-url https://example.com/{{var:64dec}}
(Added in 8.13.0)