How do I ensure the base64 encoded string is URL-safe?

Responsive Ad Header

Question

Grade: Education Subject: Support
How do I ensure the base64 encoded string is URL-safe?
Asked by:
54 Viewed 54 Answers

Answer (54)

Best Answer
(454)
Standard base64 encoding uses characters that are not URL-safe (e.g., '+', '/', '='). To make the encoded string URL-safe, replace these characters with URL-safe equivalents (e.g., '+' with '-', '/' with '_', and remove '=' padding). Node.js doesn't have a built-in function for this. You can use `.replace(/+/g, '-').replace(///g, '_').replace(/=+$/, '')` after encoding. When decoding you may need to add the padding `=` characters back to the string.