URL encoding is one of those things that works fine until it doesn't — and then you spend an hour staring at a mangled query string wondering why a space turned into a plus sign. Let's clear it up.

What URL encoding actually is

A URL can only safely contain a limited set of characters: letters, digits, and a handful of symbols like -, _, ., and ~. Everything else — spaces, ampersands, slashes in the wrong place, emoji, accented characters — has to be represented using percent-encoding.

The rule is simple: take the byte value of the character, write it in hexadecimal, and prefix it with a %. A space becomes %20. An ampersand becomes %26. Because modern URLs are UTF-8, a character like é is two bytes and encodes to %C3%A9.

When you actually need it

You need encoding any time you're putting arbitrary data into a URL. The most common cases:

  • Query parameters. Search terms, email addresses, filter values — anything a user types.
  • Path segments. If a file name or ID contains spaces or slashes, those need encoding so they don't change the URL structure.
  • Form submissions. HTML forms with application/x-www-form-urlencoded encode their fields automatically.
  • OAuth and signed requests. Signatures depend on exact byte sequences, so encoding has to be precise and consistent.

The core problem encoding solves is ambiguity. If a user searches for a&b and you drop it straight into ?q=a&b, the server sees two parameters: q=a and b=. Encode it to ?q=a%26b and the meaning is preserved.

The gotchas that actually bite

Space: %20 vs +

This is the classic. In the query string of a form submission (application/x-www-form-urlencoded), a space is encoded as +. In the path, and in strict percent-encoding, a space is %20. Most servers accept both in the query, but they are not interchangeable everywhere. If you're building a path segment, always use %20. A literal + in a query string, by the way, means a space — so to send an actual plus sign you must encode it as %2B.

Encode components, not the whole URL

A frequent mistake is encoding an entire URL at once. If you encode https://example.com/a b wholesale, you'll turn the :// and / into garbage. Encode each piece — the individual path segment or query value — then assemble the URL. In JavaScript this is the difference between encodeURIComponent() (for a single value) and encodeURI() (for a whole URL that's already structured correctly).

Double encoding

Encode something twice and %20 becomes %2520 (because the % itself gets encoded to %25). This usually happens when one layer of your stack encodes a value that an earlier layer already encoded. The symptom is literal %20 text showing up in your app instead of spaces. Decode once, check what you've got, and make sure only one layer is doing the work.

Reserved vs unreserved characters

Characters like /, ?, #, &, and = are reserved — they have structural meaning. encodeURIComponent() encodes them, which is what you want inside a value. But some characters (!, ', (, ), *) are left alone by JavaScript's built-in even though stricter specs expect them encoded. For OAuth 1.0 signatures you'll need to handle those manually.

A short worked example

Say you're building a search link where the user searched for café & tea?. Here's how it plays out in JavaScript:

const query = 'café & tea?';

// Encode just the value
const encoded = encodeURIComponent(query);
// => 'caf%C3%A9%20%26%20tea%3F'

const url = `https://example.com/search?q=${encoded}`;
// => 'https://example.com/search?q=caf%C3%A9%20%26%20tea%3F'

// On the receiving end:
const params = new URLSearchParams('q=caf%C3%A9%20%26%20tea%3F');
params.get('q');
// => 'café & tea?'

Notice how é became %C3%A9, the space became %20, the ampersand became %26, and the question mark became %3F. All the structurally dangerous characters are neutralised, and URLSearchParams decodes them back cleanly.

In other languages the tooling is similar: Python has urllib.parse.quote() and unquote(), Java has URLEncoder/URLDecoder (which uses + for spaces, so mind that), and Go has url.QueryEscape().

Debugging encoded strings

When you're staring at a long percent-encoded string trying to work out whether it's correct — or double-encoded, or using + where it should use %20 — it's much faster to just paste it into a tool and see the decoded result. Our URL Encoder / Decoder does both directions instantly, which is handy for sanity-checking what your API is actually receiving versus what you think you sent.

A quick debugging checklist when something looks wrong:

  • Decode the value once. Do you get readable text, or more percent codes? More codes means double encoding.
  • Check for stray + signs — is this a form query or a strict URL?
  • Confirm the encoding is UTF-8. Legacy systems sometimes use Latin-1, which produces different byte sequences for accented characters.
  • Make sure reserved characters inside values (&, =, #) are encoded, not left raw.

Rules of thumb to keep

  • Encode values, assemble the URL afterwards.
  • Use encodeURIComponent() (or your language's component-encoder) for individual pieces.
  • Decode exactly once, encode exactly once — no more.
  • Prefer %20 for spaces unless you're specifically building form-encoded data.
  • Always use UTF-8.

Get those right and the vast majority of encoding bugs simply never happen. The rest are usually some layer in your stack quietly encoding behind your back — and now you know exactly what to look for.

Photo by Chris Ried on Unsplash