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 (=).
| Parameter | Type | Requirement | Explanation |
|---|---|---|---|
text | string | required | The string to encode |
| Return value | Explanation |
|---|---|
| string | The 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.
| Parameter | Type | Requirement | Explanation |
|---|---|---|---|
encoded | string | required | The Base64-encoded string to decode |
| Return value | Explanation |
|---|---|
| string | The 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());