URL encoding prevents query parameters from being misread when links pass through browsers, servers, redirects, and APIs. This guide explains when to encode URL parameters, how reserved and Unicode characters change, how arrays and nested values are commonly represented, and how to debug a query string without accidentally encoding it twice.
Overview
URL encoding, also called percent encoding, represents characters in a URL using a percent sign followed by two hexadecimal digits. For example, a space can become %20, & can become %26, and a non-ASCII character such as é can become a sequence based on its UTF-8 bytes.
The important distinction is between a complete URL and a value placed inside a URL. You normally encode the individual parameter value before joining it to a query string. Encoding an entire URL after it has already been assembled can encode separators such as ?, =, and &, changing the URL's meaning.
For example, a search value should be encoded before it is inserted:
const term = 'red shoes & boots';
const url = '/search?q=' + encodeURIComponent(term);
The resulting query is /search?q=red%20shoes%20%26%20boots. The ampersand belongs to the value, so it must not be allowed to act as a separator between parameters.
Quick character reference
| Character or input | Common encoded form | Why it matters |
|---|---|---|
| Space | %20 or sometimes + | A literal space is not used directly in a URL. Form-style query encoding may use +. |
& | %26 | It separates query parameters, so it must be encoded when it is data. |
= | %3D | It separates a parameter name from its value. |
? | %3F | It begins a query when used as URL syntax. |
# | %23 | It begins a fragment and is not sent as part of a normal HTTP request. |
+ | %2B | It can be interpreted as a space by form-style parsers. |
| Unicode text | UTF-8 percent sequences | Encoding preserves characters across systems that expect ASCII URL syntax. |
Checklist by scenario
Building one query parameter
- Keep the original value as text in your application.
- Encode the value, not the parameter name and not the complete URL.
- Join it to the URL using the correct
?,=, and&separators. - Inspect the final URL to confirm that separators remain readable while data characters are escaped.
In JavaScript, encodeURIComponent() is designed for a component such as a query value. It is different from encodeURI(), which is intended for a broader URI and leaves more URL syntax characters untouched. When constructing query parameters, URLSearchParams is often clearer because it handles names, values, and separators together:
const params = new URLSearchParams();
params.set('q', 'red shoes & boots');
params.set('page', '2');
const url = '/search?' + params.toString();
Use the decoding operation only at the boundary where you need the original value. Repeatedly decoding data can create ambiguity, especially when a literal percent sequence was part of the input.
Handling spaces and Unicode
Do not assume that + and %20 are interchangeable everywhere. They are commonly treated differently by generic URI handling and form-style query parsing. A form parser may convert + to a space, while a more general URL parser may preserve it as a plus sign. If the distinction matters, encode a literal plus as %2B and use the serialization format expected by the receiving application.
Unicode values should be passed through a URL-aware encoder rather than manually replacing characters. For example, a city such as 東京 should be encoded using its UTF-8 representation by the platform or library. Test both the outgoing URL and the value after the server parses it.
Encoding arrays
There is no single universal array notation in query strings. Common conventions include repeated keys, bracket notation, and comma-separated values:
tag=red&tag=blueuses repeated keys.tag%5B%5D=red&tag%5B%5D=blueuses bracket notation.tag=red%2Cblueuses one value containing a comma.
Choose the convention documented by the receiving API or framework. Do not split on commas unless the API defines commas as a delimiter; a value such as Smith, Jones may otherwise be changed.
Encoding nested parameters
Nested data is also convention-dependent. An API might expect filter%5Bstatus%5D=active, dotted names such as filter.status=active, or a JSON value encoded inside one parameter. In the last case, serialize the object first and then encode the resulting JSON string. Never concatenate untrusted text into a JSON-bearing query parameter without using the appropriate serializer.
What to double-check
Check the boundary between syntax and data
In /items?category=books&sort=title, the question mark begins the query, the ampersand separates parameters, and the equals signs assign values. Those characters are syntax in this position. If a category value is books & magazines, the ampersand belongs to the data and must become %26.
Check what the server actually receives
Browser address bars, server logs, framework request objects, and application logs may display different representations. Compare the raw request URL with the parsed parameter value. Also verify whether a reverse proxy or redirect has rebuilt the query string. For a broader request investigation, use the request-and-response steps in the API testing workflow guide.
Use a browser debugging workflow
- Open browser developer tools and select the Network panel.
- Trigger the request with the exact input that fails.
- Inspect the request URL, query-parameter view, redirect chain, and response.
- Compare the encoded URL with the value shown in the server response or application state.
- Repeat using a simple value, a value containing spaces, and a value containing
&or+.
Use the command line to isolate application code
When you need to test an endpoint independently, let the command-line client encode the data rather than placing raw special characters in a shell command. With curl, a pattern such as curl --get --data-urlencode 'q=red shoes & boots' https://example.test/search makes the intended parameter boundary explicit. Replace the example host with a test endpoint and avoid placing credentials or sensitive tokens in shell history.
Common mistakes
- Encoding the complete URL: Applying a component encoder to
https://example.test/search?q=redcan turn its scheme, path, and separators into data. Encode components before assembly instead. - Encoding twice: A percent sign in
%26can become%2526. Trace each layer to find whether the client, router, proxy, or server performed an extra encoding pass. - Treating a plus as an ordinary space: A literal plus in a search term, phone number, or calculation may be parsed as a space by form-style decoding. Encode it when it is data.
- Assuming every framework parses arrays alike: Repeated keys, brackets, and comma lists can produce different server-side values. Match the API contract and test the actual parser.
- Reading the fragment on the server: Text after
#is generally handled by the browser and is not included in the HTTP request. Use a query parameter when the server must receive the value. - Logging sensitive query values: Query strings can appear in browser history, access logs, analytics systems, and referrer-related contexts. Prefer a safer transport for secrets and keep diagnostic logging deliberate.
When to revisit
Return to this checklist whenever you add a new filter, search form, redirect, pagination scheme, or API client. It is especially useful when a workflow changes libraries or frameworks, because serializers and server parsers may use different defaults for spaces, arrays, nested objects, and repeated keys.
Before releasing a URL-handling change, test at least these inputs:
plain-texttwo wordsred & blue10% discounta+b- one Unicode value
- an empty value and a missing value
- one array and one nested object, using the API's documented format
Finally, record the expected input and parsed output in an integration test. If a redirect, proxy, or browser storage workflow is involved, compare each boundary rather than checking only the final page. This makes broken links easier to diagnose and gives future maintainers a precise contract for how your application encodes URL parameters.