digest.base64

STRINGdigest.base64STRINGinput

Available inall subroutines.

Returns the Base64 encoding of the input string, as specified by RFC 4648 and is suitable for decoding by digest.base64_decode.

IMPORTANT: Although Base64 can encode binary data, the input to this function is treated as a string. The STRING type in VCL cannot contain NUL characters. As such, it is not possible to encode NUL characters using this function.

The alphabet used is:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789+/

The output string is padded to a multiple of four characters in length using the = character.

See digest.base64_decode for details on the structure of Base64 encoding. In particular, note that it is possible for two input strings to encode to the same Base64 string.

Example

declare local var.base64_encoded STRING;
set var.base64_encoded = digest.base64("Καλώς ορίσατε");
# var.base64_encoded is now "zprOsc67z47PgiDOv8+Bzq/Pg86xz4TOtQ=="

Try it out

digest.base64 is used in the following code examples. Examples apply VCL to real-world use cases and can be deployed as they are, or adapted for your own service. See the full list of code examples for more inspiration.

Click RUN on a sample below to provision a Fastly service, execute the code on Fastly, and see how the function behaves.

Send HTTP Basic Auth in request to origin

Convert a password sent by the client in the querystring into a Authorization header to your origin server.

Cache POST/GraphQL query responses

GraphQL query requests are POSTs, but responses to POST typically can't be cached. Convert it to a querystring on a GET request to allow Fastly to cache GraphQL (or any HTTP POST) request.