How to URL-Encode a String for a Query Parameter
You build a URL like /search?q=cats & dogs and it falls apart: the space breaks the link and the & gets read as the start of a new parameter. The fix is URL encoding(also called percent-encoding) — turning unsafe characters into % escape sequences so the value survives intact.
Here is how to encode a query-parameter value correctly.
How to URL-encode a query parameter
QuickWand's free URL Encoder / Decoder encodes in your browser — the text you paste, tokens and all, is never uploaded.
- Open the URL Encoder.
- Paste just the value — the part that goes after the
=— not the whole URL. For examplecats & dogs. - Click Encode. You get back
cats%20%26%20dogs, safe to drop into your query string. - Assemble the URL:
/search?q=cats%20%26%20dogs. To go the other way, paste an encoded value and click Decode.
Encode the value, not the whole URL
This is the mistake that bites people. There are two encoding functions, and they exist for two different jobs:
encodeURIComponent— encodes a single component, escaping the structural characters&,=,?,/,#, and more. This is the right choice for a query-parameter value, because you want a literal&inside your value to become%26so it is not mistaken for a parameter separator.encodeURI— encodes a whole URL and deliberately leaves&,=,?, and/alone so the URL's structure stays intact. Using it on a single value would leave those characters dangerous.
Rule of thumb: building a URL from a template? Encode each value with encodeURIComponent first, then slot the encoded pieces into the structure. The URL Encoder above does the encodeURIComponent-style job, so paste values, not whole links.
%20 vs + for spaces
You will see a space encoded two ways. In percent-encoding a space is %20, and that is always safe anywhere in a URL. In the query string of an HTML form submission (the application/x-www-form-urlencoded format), a space is conventionally written as +. Both decode back to a space within a query string, but a literal + in a path stays a plus. When in doubt, %20 never surprises you.
Common characters and their encodings
- space →
%20 &→%26=→%3D?→%3F/→%2F#→%23
Decoding values you find in the wild
The reverse is just as common: you copy a URL out of a log or an API response and it is full of %20 and %3D noise. Paste it into the decoder to read it. And if a query parameter turns out to hold a Base64 blob, hand that off to the Base64 decoder — query strings frequently carry encoded payloads inside an already-encoded value.