TechCompare LogoTechCompare

URL-safe Base64 vs standard Base64: why your JWT tokens need the variant

Use URL-safe Base64 for anything that appears in a URL: JWT tokens, OAuth state parameters, verification tokens in emails, and data URIs embedded in SVG. Standard Base64 is for everything else: JSON payloads, MIME attachments, and storage.

Standard Base64 uses + and / in its 64-character alphabet. Those characters have special meaning in URLs (+ is a space in query strings, / is a path separator), so putting standard Base64 in a URL requires percent-encoding. URL-safe Base64 (RFC 4648 §5) replaces + with - and / with _, making the output safe to drop directly into a URL path or query string without any encoding.

By TechCompare · Updated

Encoding focus
URL-safe Base64
url-safe-base64
Category
Best Practices
Practical encoding guidance

How this is calculated

JWT tokens use URL-safe Base64 for all three segments (header, payload, signature). Without it, every JWT would need to be percent-encoded before appearing in an Authorization: Bearer header or a query parameter. URL-safe Base64 also commonly omits the = padding characters, since the encoded length can be inferred from the string length. JavaScript's btoa() produces standard Base64. To get URL-safe, replace + with -, / with _, and strip trailing =. Most JWT libraries handle this automatically, but if you're hand-rolling token generation, this is the most common mistake.

Verdict

Standard Base64 leaks two characters that break in URLs: + reads as a space in query strings and / is a path separator. RFC 4648 §5 swaps them for - and _ so the output drops in cleanly. JWT encodes all three segments this way, and trailing = padding is usually stripped since the length implies it. JavaScript's btoa() still emits standard output, so swap manually or use a library.

More Encoding scenarios

Base64 vs Hex
Base64 and hexadecimal both encode binary data as text, but they serve different purposes.
View details ➜
UTF-8 vs ASCII
ASCII maps 128 English characters to 7-bit values.
View details ➜
URL encoding guide
Percent-encoding (also called URL encoding) replaces characters that aren't safe in a URL with a percent sign followed by two hex digits.
View details ➜

Frequently asked questions

What's the difference between standard Base64 and URL-safe Base64?
Two characters. Standard Base64 uses + and / in its alphabet, and both break in URLs: + decodes as a space in query strings and / splits paths. URL-safe Base64 (RFC 4648 §5) swaps them for - and _, so the output drops into a URL path or query parameter without any percent-encoding.
Why do JWT tokens use URL-safe Base64 instead of standard Base64?
Because JWTs travel in Authorization headers, query parameters, and redirect URLs, where a raw + or / would corrupt the token. URL-safe encoding lets all three JWT segments (header, payload, signature) ride through those channels untouched. Most JWT libraries also strip the = padding, since the segment lengths imply it.
How do I convert btoa() output to URL-safe Base64 in JavaScript?
btoa() emits standard Base64, so swap characters afterward: replace every + with -, every / with _, and trim trailing = characters. Reversing on decode means restoring the swaps and re-adding padding to a multiple of 4. Every mainstream JWT or encoding library already does this, so hand-roll it only when a library isn't an option.