Decode URL Safe Base64 in JavaScript (browser side)

wibberding picture wibberding · Jan 23, 2015 · Viewed 16.1k times · Source

I am using Gmail's API to get emails from my account. The body of the message is delivered in a "URL safe base64" format. What is the best way to decode this for use? I have found some nodejs solutions, but no client side ones. window.atob does not work since it is URL safe.

Thanks for any help.

Answer

xori picture xori · Jun 13, 2017

For posterity,

atob(data.replace(/_/g, '/').replace(/-/g, '+'))

As stated by the spec https://tools.ietf.org/html/rfc4648#section-5 however because this uses atob() it does not support unicode characters and thus requires a polyfill.