Spaces turning into %20, plus signs showing up where you didn't put them, query params that break the moment someone types an ampersand in a search box — nearly every one of these bugs comes back to URL encoding. It's a small topic that quietly causes a surprising amount of grief.

What URL encoding actually is

URLs are only allowed to contain a limited set of characters: letters, digits, and a handful of symbols like -, ., _ and ~. Everything else — spaces, slashes used as data, accented characters, emoji, ampersands inside a value — has to be represented some other way.

URL encoding (also called percent-encoding) solves this by replacing an unsafe character with a % followed by its byte value in hexadecimal. A space becomes %20. An ampersand becomes %26. Non-ASCII characters get encoded as their UTF-8 bytes, so é becomes %C3%A9.

The point is to make sure the parser reading the URL can tell structure from data. Without it, a question mark in your data is indistinguishable from the question mark that starts the query string.

When you actually need it

The common cases:

  • Query string values — anything a user types that ends up in ?key=value. Search terms, email addresses, filter strings.
  • Path segments built from data — a product name or username in the path, like /users/John%20Doe.
  • Form submissions — application/x-www-form-urlencoded bodies use the same rules.
  • Redirect and callback URLs — when you pass one URL as a parameter inside another (OAuth flows love this).

You do not encode the whole URL blindly. The reserved characters that give a URL its structure — the ://, the ?, the & between params — need to stay as-is. Encoding them would break the URL. You encode the pieces, then assemble them.

The two functions people mix up

In JavaScript there are two built-ins and choosing wrong is a classic bug.

  • encodeURIComponent() — encodes almost everything, including /, ?, &, =. Use this for individual values.
  • encodeURI() — leaves reserved structural characters alone. Use this only when you have a complete URL that's already mostly valid and you just want to tidy up spaces and the like.

Rule of thumb: if you're building a query parameter value, reach for encodeURIComponent every time.

const base = "https://api.example.com/search";
const q = "coffee & tea";
const filter = "price>10";

// Correct: encode each value
const url = `${base}?q=${encodeURIComponent(q)}&filter=${encodeURIComponent(filter)}`;
// https://api.example.com/search?q=coffee%20%26%20tea&filter=price%3E10

// Wrong: the raw & splits your value into two params
const broken = `${base}?q=${q}`;
// q now equals "coffee " and " tea" becomes a stray param

Use URLSearchParams when you can

Hand-building query strings is error-prone. The URLSearchParams API handles encoding for you:

const params = new URLSearchParams({
  q: "coffee & tea",
  filter: "price>10"
});
const url = `https://api.example.com/search?${params}`;
// everything encoded correctly, no manual escaping

Gotchas that bite in production

The plus-sign trap

In the application/x-www-form-urlencoded format, a space is encoded as +, not %20. In a normal URL path or with encodeURIComponent, a space is %20 and a literal + means a plus sign. This mismatch is why a search for "C++" sometimes comes back as "C " — a server decoded the + characters as spaces. If you're passing a value that might contain a literal plus, encode it to %2B and make sure both ends agree on the rules.

Double encoding

Encoding something that's already encoded turns %20 into %2520 (because the % itself gets encoded to %25). This happens when a value passes through two layers that each helpfully encode it. If you see %25 showing up unexpectedly, you've got a double-encoding bug. Encode once, as late as possible, right before the value goes into the URL.

Encoding the whole URL as one string

Running encodeURIComponent over a full URL destroys its structure. The colons and slashes get escaped and the result is useless as a link. Only encode the dynamic parts.

Server-side decoding differences

Not every framework decodes query strings identically, especially around +, repeated keys, and non-UTF-8 bytes. When you're debugging an API that "loses" characters, log the raw incoming request line before any framework parsing touches it.

A quick worked example

Say you're building an OAuth redirect and need to pass a return URL as a parameter. The return URL itself has a query string:

const returnTo = "https://app.example.com/dashboard?tab=billing&ref=email";

const authUrl =
  "https://auth.example.com/login?redirect_uri=" +
  encodeURIComponent(returnTo);

// Result:
// https://auth.example.com/login?redirect_uri=
//   https%3A%2F%2Fapp.example.com%2Fdashboard%3Ftab%3Dbilling%26ref%3Demail

Notice how the inner ?, &, and : all become %3F, %26, and %3A. That's exactly what you want — the whole return URL is now a single opaque value that won't be mistaken for part of the auth URL's own query string. When the auth server reads redirect_uri, it decodes it back into the original.

If you'd skipped the encoding, the auth server would have parsed tab=billing and ref=email as its own parameters and your redirect would have been truncated at the first &.

Checking your work by hand

When something looks off, it helps to decode a string manually and see what's really in there. I keep a URL Encoder / Decoder open for exactly this — paste in a suspicious parameter, decode it, and the double-encoding or stray + problems usually jump straight out. It's faster than firing up a REPL just to confirm whether %2520 means what you think it means.

The short version

  • Encode the values, never the whole URL structure.
  • Use encodeURIComponent for query parameters and path data.
  • Prefer URLSearchParams to building strings by hand.
  • Watch for + vs %20 and for %25 signalling double encoding.
  • Encode once, decode once, and make sure both ends of the wire agree on the rules.

Get those right and the vast majority of mysterious "my parameter disappeared" bugs simply stop happening.

Photo by Pankaj Patel on Unsplash