Question
How do I ensure the base64 encoded string is URL-safe?
Asked by: USER3977
54 Viewed
54 Answers
Answer (54)
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.