Skip to content

Base64Encode / Base64Decode

SyncJS provides two complementary functions for encoding and decoding data using standard Base64 (RFC 4648).

NOTE

These functions are available as of Syncplify Server! v7.1.1. If you are running an older version, upgrade to v7.1.1 or later to use them.

Base64Encode

ts
function Base64Encode(text: string): string;

Encodes the string text using standard Base64 and returns the encoded result. The output uses the standard alphabet (A-Z, a-z, 0-9, +, /) with padding characters (=).

ParameterTypeRequirementExplanation
textstringrequiredThe string to encode
Return valueExplanation
stringThe Base64-encoded form of the input

Example

ts
var encoded = Base64Encode('Hello, World!');
// encoded === "SGVsbG8sIFdvcmxkIQ=="

Base64Decode

ts
function Base64Decode(encoded: string): string;

Decodes a standard Base64-encoded string and returns the original text. Returns an empty string if the input is not valid Base64.

ParameterTypeRequirementExplanation
encodedstringrequiredThe Base64-encoded string to decode
Return valueExplanation
stringThe decoded string, or "" if the input is invalid

Example

ts
var decoded = Base64Decode('SGVsbG8sIFdvcmxkIQ==');
// decoded === "Hello, World!"

Practical use: Authorization header

ts
var credentials = Base64Encode('alice:my-password');
var hc = new HttpCli();
var res = hc.Url('https://api.example.com/data')
             .Header('Authorization', 'Basic ' + credentials)
             .Get();
Log('Status: ' + res.StatusCode());