DevRivet

Blog ·

URL Encode a Query Parameter with Special Characters

Encode a query parameter value with encodeURIComponent so &, =, #, + and non-ASCII characters survive. Step-by-step examples and the output to expect.

URL Encoder / Decoder

Encode and decode URL/query-string components.

Try it free →

To URL encode a query parameter value, pass the raw value through encodeURIComponent() in JavaScript, or urllib.parse.quote(value, safe='') in Python. Both percent-encode every character that has a special meaning in a URL, so the value can't be mistaken for the end of the parameter or the start of a new one.

To check your output as you go, paste the raw value into the URL Encoder / Decoder and compare the result with the examples below.

Why special characters break query strings

A query string is a list of key=value pairs separated by &. Everything after # is a fragment. Those characters are structural, so they need special handling when they appear inside a value.

  • & separates parameters. A raw & in a value starts a new parameter.
  • = separates a key from its value. A raw = in a value is usually accepted by browsers, but it makes the pair harder to parse correctly, and some parsers split on the first = and some on every =.
  • # starts the fragment. Everything after a raw # is dropped by the browser and is never sent to the server.
  • + means a space in form-encoded query strings. A raw + in a value often arrives as a space.
  • Non-ASCII characters such as é or emoji must be encoded as UTF-8 bytes, each written as %XX.

Take this search term: Tom & Jerry's café #1 + extras=yes

If you append it without encoding, you get this string:

https://example.com/search?q=Tom & Jerry's café #1 + extras=yes

That string is not a valid URL. It contains raw spaces and a non-ASCII character, and the characters after the & and # are interpreted in ways you did not intend. The & splits the value, so q receives only 'Tom ' with a trailing space, because the space before the & is kept. The text after the & becomes a separate key, ' Jerry's café ', with surrounding spaces and no value. Everything from the # onward is a fragment. The browser drops it, so the server never sees #1 + extras=yes at all.

Step 1: Encode the value with encodeURIComponent

In JavaScript, encodeURIComponent() is the right function for a single value. It escapes everything except letters, digits and the characters - _ . ! ~ * ' ( ).

const raw = "Tom & Jerry's café #1 + extras=yes";
const encoded = encodeURIComponent(raw);
console.log(encoded);
// Tom%20%26%20Jerry's%20caf%C3%A9%20%231%20%2B%20extras%3Dyes

Look at each special character:

  • & becomes %26
  • = becomes %3D
  • # becomes %23
  • + becomes %2B
  • space becomes %20
  • é becomes %C3%A9, the two UTF-8 bytes for the character

The apostrophe in Jerry's stays as-is, because encodeURIComponent() does not escape '. That is valid inside a query value. If your target system is strict and you want it escaped too, replace it yourself, but most servers accept it.

Step 2: Append the encoded value to the query string

Encode each value separately, then join the pairs with & yourself. Do not encode the whole URL, or the ? and & separators will be encoded too.

const base = "https://example.com/search";
const params = {
  q: "Tom & Jerry's café #1 + extras=yes",
  page: "2"
};
const query = Object.entries(params)
  .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
  .join("&");
const url = `${base}?${query}`;
console.log(url);
// https://example.com/search?q=Tom%20%26%20Jerry's%20caf%C3%A9%20%231%20%2B%20extras%3Dyes&page=2

In practice, URLSearchParams does this for you and handles the encoding of each pair:

const url = new URL("https://example.com/search");
url.searchParams.set("q", "Tom & Jerry's café #1 + extras=yes");
url.searchParams.set("page", "2");
console.log(url.toString());
// https://example.com/search?q=Tom+%26+Jerry%27s+caf%C3%A9+%231+%2B+extras%3Dyes&page=2

URLSearchParams uses form encoding, so the apostrophe is escaped as %27 and spaces appear as +. Both forms decode to the same value in a standard form parser. Pick one style per project and stick with it. For the space details, see URL Encode Space: Why %20 or + Shows Up in URLs.

Step 3: Python equivalent

In Python, use urllib.parse.quote with safe='' so that the default safe characters (including /) are also encoded. For form-style output with +, use urlencode.

from urllib.parse import quote, urlencode

raw = "Tom & Jerry's café #1 + extras=yes"
print(quote(raw, safe=''))
# Tom%20%26%20Jerry%27s%20caf%C3%A9%20%231%20%2B%20extras%3Dyes

print(urlencode({'q': raw, 'page': 2}))
# q=Tom+%26+Jerry%27s+caf%C3%A9+%231+%2B+extras%3Dyes&page=2

Python's quote() with default arguments keeps / unescaped and escapes the apostrophe as %27. Always pass safe='' when the input is a value, not a path.

Verify the result by decoding it

A quick round trip confirms the encoding. Decoding the output must give back the exact raw string.

const encoded = encodeURIComponent(raw);
console.log(decodeURIComponent(encoded) === raw); // true

Also check the byte length of non-ASCII characters. The café example uses two bytes for é, so it appears as %C3%A9. If you see a single %E9 for é, the value was encoded as Latin-1 instead of UTF-8, and the receiving server will likely show garbled text.

Common pitfalls

  • Encoding the whole URL. encodeURIComponent('https://example.com/?a=1') turns : / ? = into escapes and the link stops working. Encode values only.
  • Double encoding. If a value is already encoded and you encode it again, %20 becomes %2520. Decode once at the boundary, or encode once at the edge.
  • Forgetting the keys. Keys with special characters also need encoding, though most keys are plain ASCII.
  • Assuming + is literal. On the server side, many frameworks decode + as a space. If you need a literal plus, send %2B.
  • Non-UTF-8 back ends. Some legacy systems expect a different charset. Check what the server expects before you pick an encoding.

DevRivet's URL Encoder runs entirely in your browser, which matters when the value contains tokens, API keys or personal data. You can test sensitive strings there without sending them to a server.

Quick reference

  • & → %26
  • = → %3D
  • # → %23
  • + → %2B
  • ? → %3F
  • / → %2F with encodeURIComponent() or quote(value, safe=''); Python quote() leaves / unescaped by default
  • é → %C3%A9
  • space → %20 with encodeURIComponent, + with URLSearchParams

If you also need to prepare test values for the same request, the Unix Timestamp Converter and UUID Generator can help. For encoding data for a header or body rather than a URL, see Base64 vs Base64URL.

FAQ

Should I encode the whole URL or only the parameter values?

Only the values, and the keys if they are dynamic. The scheme, host, path, ? and & separators must stay unencoded, or the URL will not work. Build the query from encoded pieces, as shown above.

Do I need to encode the apostrophe or parentheses?

Not usually. encodeURIComponent leaves ' ( ) unescaped, and they are valid in query values. Encode them only if a downstream system requires it.

Can I put a # inside a query value if I encode it?

Yes. Once # is encoded as %23, it is part of the value and the server receives it. A raw # always starts the fragment, so it must never appear unencoded in a value.

URL Encoder / Decoder

Encode and decode URL/query-string components.

Try it free →