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). JOSE requires unpadded Base64url for these segments. That is a token-format rule, not a restriction of the Authorization header: RFC 6750 permits +, /, and trailing = in Bearer credentials without percent-encoding. URL components have separate escaping rules. 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) represents a byte with a percent sign followed by two hex digits when that byte can't appear literally in the URL component being built.
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 the JOSE format specifies unpadded Base64url for JWT segments. Ordinary Base64 characters do not corrupt a Bearer header: RFC 6750 allows +, /, and trailing = there. URLs have separate escaping requirements, so use a URL builder when inserting token values.
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.