Skip to content
Developer

4 min read

Use URL Encoding and Base64 for the Right Problem

Understand how URL components and Base64 differ, avoid double encoding, and handle Unicode without mistaking encoding for security.

By GoldieKit · Published

Text passing through two different encoding paths

URL encoding and Base64 both turn data into characters that are easier to transport in a particular context, but they solve different problems. Percent encoding protects the structure of a URL component: a space, ampersand, or slash in a value should not accidentally become a separator. Base64 represents bytes using a limited alphabet. Neither hides data from someone who can read or decode it. GoldieKit offers a URL encoder and a Base64 encoder for local experiments.

URL structure determines what to encode

A URL has a scheme, host, path, query, and fragment. Encoding an entire URL as though it were one query value can turn its structural slashes and colon into data. Conversely, inserting an unencoded value into a query string can let & start a new parameter or # start a fragment. Decide whether you are encoding a component or a complete URL before using a tool or API.

For example, a search value cats & dogs should be encoded as a query parameter value so the ampersand stays within the value. In JavaScript, URLSearchParams can build the query safely. If the text is a full URL you want to place inside another URL's query parameter, encode it as that parameter's value, not by modifying the original destination's host and path blindly.

const target = new URL('https://example.org/search');
target.searchParams.set('q', 'cats & dogs');

The browser's URL APIs handle the context more reliably than manual string concatenation. Use URL parser to inspect the resulting host, path, query, and fragment. A visually plausible URL can still point to an unexpected host if characters were placed in the wrong part.

Base64 starts with bytes

Base64 maps bytes into characters such as letters, digits, +, /, and padding = in the common form. To encode text, first decide its character encoding, typically UTF-8, then encode the resulting bytes. Decoding reverses that process. If you decode bytes as the wrong character set, non-ASCII text can become garbled even though the Base64 itself is valid.

Base64 commonly appears in data URLs, email bodies, and binary fields of text-based protocols. It increases the character length by roughly one third before any surrounding syntax. It is not compression and not encryption. The Base64 article covers its cost and common uses in more detail.

Need Better starting point
Put A&B in one query value URL component encoding
Put binary bytes in a text field Base64, if the protocol requires it
Hide a secret Encryption or access control, not either encoding
Make an arbitrary URL safe to open Validate its destination and scheme

Avoid double encoding

If %20 is already the encoded form of a space, encoding the percent sign again can produce %2520. The receiver decodes once and sees the literal text %20 rather than a space. This often happens when one layer builds a query string and another treats the entire string as a raw value. Keep data as unencoded strings until the URL API inserts it at the right boundary. Document which layer owns encoding.

Base64 has a similar boundary problem: encoding an already encoded string produces a different payload. A decoder may succeed but return a Base64-looking string instead of the original bytes. Inspect the decoded result and protocol specification rather than repeatedly pressing encode or decode until something looks plausible.

Security and privacy limits

Both forms are reversible. Do not place passwords, tokens, or private data in a URL merely because the value is percent-encoded or Base64-encoded. URLs can appear in browser history, logs, screenshots, and referrer flows. A Base64 string can be decoded by anyone who receives it. GoldieKit's tools process the input in the browser tab; that does not change the risk of sharing the resulting string elsewhere.

When accepting a URL from someone else, inspect the scheme and host before opening it. An encoded path can be deceptive, and a shortened link can hide the destination. URL parser helps split the parts, but it does not certify the site's trustworthiness.

A reliable working rule

Use URL APIs for URL components and query parameters. Use Base64 only when a format or protocol needs bytes represented as text. Specify UTF-8 or another actual character encoding, keep ownership of encoding to one layer, and test a value containing spaces, &, non-ASCII letters, and punctuation. If the purpose is secrecy or authenticity, choose a real security control instead of an encoding.

Suppose a help page has a next parameter containing https://example.org/docs?topic=A&B. If you concatenate the inner URL directly after ?next=, the outer parser can treat &B as another outer parameter. Add the complete inner URL as the value of next through URLSearchParams. The outer URL will encode the characters needed to preserve that value. When reading it, parse the outer URL first, retrieve next, then validate the retrieved destination before navigating. Correct encoding preserves structure; it does not make an arbitrary redirect safe.


Put the ideas to work

Focused guides

For a shorter, step-by-step walkthrough:


Keep exploring

All guides