Base64 vs URL Encoding: What's the Difference?

04 Jan 2026 1,674 words

Base64 vs URL Encoding

Base64 encoding and URL encoding (percent encoding) serve different purposes in web development. Understanding when to use each is essential. While both are encoding schemes that transform data into a different representation, they solve fundamentally different problems. Base64 is designed to convert arbitrary binary data into a safe ASCII text format, while URL encoding ensures that special characters in URLs are transmitted safely over the internet. Choosing the wrong encoding for your use case can lead to data corruption, broken URLs, security vulnerabilities, or unnecessarily inflated payload sizes.

Developers frequently encounter situations where they must decide between these two encoding methods. For example, when including an authentication token in a URL query parameter, you might wonder whether to use Base64 encoding or URL encoding. The answer depends on the nature of the data, the constraints of the transport medium, and the requirements of the receiving system. This guide provides a comprehensive comparison to help you make the right choice.

Understanding URL Encoding

URL encoding, also known as percent encoding, converts characters into a format that can be transmitted over the internet. The URL specification (RFC 3986) defines which characters are allowed unreserved in URLs and which must be encoded. Unreserved characters include uppercase and lowercase letters, digits, and the characters hyphen (-), underscore (_), period (.), and tilde (~). All other characters must be percent-encoded if they appear in a URL.

Spaces become %20, and special characters are replaced with their hexadecimal ASCII values preceded by %. The encoding process is straightforward: each byte that is not an unreserved character is replaced with a % followed by its two-digit hexadecimal representation. For example, the string hello world becomes hello%20world, and a&b=c becomes a%26b%3Dc.

Why URL Encoding Is Necessary

URLs have a constrained character set for historical and practical reasons. The original URL specification was designed when the internet primarily transmitted 7-bit ASCII text. Characters outside this range, or characters that have special meaning in URLs (like ?, #, and &), must be encoded to prevent them from being misinterpreted by the URL parser.

For example, the & character is used to separate query parameters. If your data contains an &, it would be interpreted as a parameter separator rather than as data. URL encoding converts & to %26, ensuring it is treated as part of the parameter value. Similarly, the # character marks the beginning of a URL fragment; %23 ensures it appears as data.

URL encoding also enables the inclusion of non-ASCII characters in URLs through UTF-8 encoding. For example, the Unicode character U+00E9 (é) is encoded as %C3%A9 in a URL. This allows internationalized domain names and paths to be represented within the ASCII-only URL specification.

Common URL Encoded Characters

Character Encoded Character Encoded
Space %20 # %23
! %21 $ %24
" %22 % %25
& %26 + %2B
, %2C / %2F
: %3A ; %3B
= %3D ? %3F

The space character deserves special mention because it has two possible encodings. In query strings, the application/x-www-form-urlencoded specification encodes spaces as + rather than %20. This legacy behavior comes from HTML form submission. When encoding data for query parameters, you should use + for spaces if you are following the form encoding convention, or %20 for spaces in other URL components like the path.

URL Encoding in Different URL Components

Different parts of a URL have different encoding requirements. The path component should not encode / because it separates path segments. The query component should not encode ? or & because they have special meanings in the query string. However, if your data contains these characters, they must be encoded: ? becomes %3F, & becomes %26.

The fragment component (after #) has the most lenient encoding rules because the fragment is never sent to the server. However, encoding is still recommended to avoid ambiguity in client-side parsing.

Understanding Base64 Encoding

Base64 converts binary data into ASCII text using a 64-character alphabet. The alphabet consists of A-Z, a-z, 0-9, +, and /, with = used for padding. This 64-character set ensures that encoded output consists only of universally safe ASCII characters, though the + and / characters require additional URL encoding when used in URLs.

Base64 encoding works by processing input data in groups of 3 bytes (24 bits). These 24 bits are split into four 6-bit groups, and each 6-bit value (0-63) is mapped to a character in the Base64 alphabet. If the input length is not a multiple of 3 bytes, padding characters (=) are added to make the output length a multiple of 4 characters.

The primary purpose of Base64 encoding is to make binary data safe for text-based transport channels. Email (MIME), JSON, XML, and HTTP headers are all text-based protocols that cannot handle raw binary data reliably because binary bytes may be interpreted as control characters or may be modified by the transport layer.

Key Differences

The fundamental differences between Base64 and URL encoding stem from their different purposes and design constraints.

Feature Base64 URL Encoding
Purpose Binary to text URL-safe text
Output size ~33% larger Variable
Character set A-Z, a-z, 0-9, +, /, = % followed by hex codes
Reversible Yes Yes
Use case Data URIs, email, API Query params, form data
Input type Binary data Text with special chars

Base64 always expands the data by approximately 33 percent regardless of the input content, because every 3 input bytes become 4 output characters. URL encoding expands the data by a variable amount. ASCII letters and digits are not expanded at all (1 byte becomes 1 byte). Spaces expand from 1 byte to 3 bytes (%20). Characters outside the ASCII range, encoded as UTF-8, expand even more: a single Unicode character might become 2 or 3 UTF-8 bytes, each encoded as %XX, resulting in 6 or 9 bytes in the URL.

The character sets also differ significantly. Base64 output uses a fixed set of 65 characters, while URL encoding can produce any character in the form %XX. This means Base64 output is more compact for binary data but cannot represent characters outside its alphabet without secondary encoding. URL encoding is more flexible but less space-efficient for binary data.

URL-Safe Base64 (Base64URL)

Because standard Base64 uses + and / as part of its alphabet, Base64-encoded data cannot be used directly in URLs without additional URL encoding. To address this, the Base64URL variant was introduced. Base64URL replaces + with - and / with _, and strips the padding = characters. These substitutions produce output that is safe for URLs without needing percent encoding.

Base64URL is used by JWT (JSON Web Tokens), which encode their header and payload using this variant. When you see a JWT token like eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dQw4w9WgXcQ, the first two segments are Base64URL-encoded.

When to Use Each

The following table provides quick guidance for common scenarios.

Scenario Encoding
Embedding images in HTML Base64
Sending data in query parameters URL encoding
Email attachments Base64
Form submissions URL encoding
API authentication tokens Base64
File paths in URLs URL encoding
JWT tokens Base64URL
Cookie values URL encoding

Choosing the Right Encoding

Follow these guidelines when deciding between Base64 and URL encoding.

Use Base64 when:

  • You need to transmit binary data (images, documents, encrypted data) through a text-based protocol.
  • You want to embed data inline in HTML, CSS, or JSON (data URIs).
  • You are encoding data for MIME email attachments.
  • You are creating authentication tokens or other opaque data blobs.

Use URL encoding when:

  • You are constructing URLs or query strings with special characters.
  • You are processing form data submitted via application/x-www-form-urlencoded.
  • You need to encode text data for use in URL path segments, query parameters, or fragments.
  • You are encoding cookie values that may contain special characters.

Combining Encodings

In some cases, you may need to use both encodings together. For example, if you are passing a Base64-encoded value as a query parameter, you must URL-encode the Base64 output to ensure that the + and = characters are safe in the URL. This double encoding is common in API designs where tokens or identifiers are Base64-encoded and transmitted as query parameters.

const base64Data = btoa('some binary data');
const urlSafe = encodeURIComponent(base64Data);
// urlSafe is now safe for use in a URL

On the receiving end, you reverse the process: first URL-decode, then Base64-decode.

Practical Examples

Example 1: Embedding an Image in HTML

You have a 1 KB PNG icon that you want to embed in an HTML email. The correct approach is Base64 encoding:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...">

URL encoding would not help here because email HTML does not natively support percent-encoded data URIs.

Example 2: Passing a Search Query in a URL

You want to create a search link that includes the query "café & bakery". The correct approach is URL encoding:

https://example.com/search?q=caf%C3%A9+%26+bakery

Using Base64 for this would produce a much longer and less readable URL.

Example 3: API Authentication Token

Your API uses a token that combines a user ID and a timestamp, signed with an HMAC. The token is binary and must be transmitted as a query parameter. The correct approach is Base64 (preferably Base64URL) followed by URL encoding, or simply Base64URL if the transport layer handles the remaining special characters.

https://api.example.com/data?token=eyJ1c2VySWQiOjEyMywidGltZXN0YW1wIjoxNzA0MDAwMDAwfQ

Using URL encoding directly on the binary token would produce a much longer result.


About this article

Understand the key differences between Base64 encoding and URL encoding, and when to use each in your projects.

Help2Code Logo
Menu