Skip to main content
The k6/encoding module provides base64 encoding and decoding functionality as defined by RFC4648.

Import

import encoding from 'k6/encoding';

Functions

b64encode()

Encodes data in base64 format.
input
string | ArrayBuffer
required
The input string or ArrayBuffer object to base64 encode.
encoding
string
default:"std"
The base64 encoding to use:
  • "std" - Standard encoding with = padding and +// characters (default)
  • "rawstd" - Standard encoding without = padding
  • "url" - URL-safe encoding with -/_ instead of +//
  • "rawurl" - URL-safe encoding without = padding
return
string
The base64 encoding of the input data.
import { check } from 'k6';
import encoding from 'k6/encoding';

export default function () {
  const str = 'hello world';
  const enc = 'aGVsbG8gd29ybGQ=';
  check(null, {
    'is encoding string correct': () => encoding.b64encode(str) === enc,
  });
}

b64decode()

Decodes a base64 encoded string into the original unencoded input.
input
string
required
The string to base64 decode.
encoding
string
default:"std"
The base64 encoding to use:
  • "std" - Standard encoding with = padding and +// characters (default)
  • "rawstd" - Standard encoding without = padding
  • "url" - URL-safe encoding with -/_ instead of +//
  • "rawurl" - URL-safe encoding without = padding
format
string
Output format:
  • "s" - Return as string
  • Unspecified - Return as ArrayBuffer (default)
return
ArrayBuffer | string
The base64 decoded version of the input in either string or ArrayBuffer format.
import { check } from 'k6';
import encoding from 'k6/encoding';

export default function () {
  const str = 'hello world';
  const enc = 'aGVsbG8gd29ybGQ=';
  check(null, {
    'is decoding to string correct': () => encoding.b64decode(enc, 'std', 's') === str,
  });
}

Encoding Types

Standard (std)

Default base64 encoding with = padding characters and +// in the alphabet.

Raw Standard (rawstd)

Standard encoding without = padding characters.

URL-safe (url)

URL-safe encoding using - and _ instead of + and /, with padding.

Raw URL-safe (rawurl)

URL-safe encoding without = padding characters.

Use Cases

  • Encoding binary data for HTTP headers or URLs
  • Encoding authentication credentials (Basic Auth)
  • Working with Base64-encoded API payloads
  • Converting between binary and text representations
  • Handling file uploads as base64 strings
Use URL-safe encoding (url or rawurl) when the encoded data will be used in URLs or filenames to avoid special characters.

Build docs developers (and LLMs) love