URL encoding problems often begin with a simple mix-up: treating a complete URL, a path segment, and a query parameter as if they required the same operation. This reference explains how to encode and decode each component safely, inspect malformed query strings, handle spaces, reserved characters, Unicode, repeated parameters, and plus signs, and verify links with JavaScript or command-line tools.
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 be represented as %20, while an ampersand becomes %26. Encoding is necessary when a value contains characters that have a special meaning in a URL or cannot be transmitted reliably in their literal form.
The most important rule is to encode the smallest correct component. Use a URL parser for a complete URL, encode a query parameter value before placing it in a query string, and encode a path segment before adding it to a path. Do not repeatedly encode an already encoded value unless the receiving system explicitly requires another layer of encoding.
| Character or value | Common encoded form | Why it matters |
|---|---|---|
| Space | %20 | Separates words but is not safe as a literal URL character. |
& | %26 | Separates query parameters, so it must be encoded inside a value. |
= | %3D | Separates a parameter name from its value. |
? | %3F | Introduces a query string when used in a URL. |
# | %23 | Introduces a fragment and is not sent to the server as part of the request. |
+ | %2B | May be interpreted as a space by form-style query parsers. |
| Unicode characters | UTF-8 percent encoding | Preserves non-ASCII text across URL boundaries. |
Reserved characters are not always wrong in a URL. Their meaning depends on where they appear. An ampersand can correctly separate two query parameters, but an ampersand inside a parameter value must be encoded. This context-sensitive behavior is why a general “encode the whole URL” operation can damage an otherwise valid link.
Checklist by scenario
Building a URL with query parameters
- Start with the base URL and keep its scheme, host, path, and existing query structure intact.
- Encode each parameter value separately rather than concatenating raw user input.
- Use a URL API or a framework query builder to add parameters.
- Preserve repeated parameters when the receiving API uses them, such as
tag=html&tag=css. - Inspect the final URL and confirm that separators such as
&and=appear only where intended.
In JavaScript, URLSearchParams is usually the clearest choice for query data:
const url = new URL("https://example.com/search");
url.searchParams.set("q", "red & blue");
url.searchParams.append("tag", "frontend");
url.searchParams.append("tag", "api");
console.log(url.toString());The API handles parameter boundaries and percent encoding. Use set() when a name should have one value and append() when repeated values are meaningful.
Encoding one path segment
Path segments have different rules from query values. If an identifier can contain a slash as data, encode it before inserting it into the path:
const documentId = "reports/2026 final";
const path = "/documents/" + encodeURIComponent(documentId);
console.log(path);This prevents the slash in the identifier from being mistaken for a path separator. Conversely, do not pass an entire URL to encodeURIComponent(); it will encode structural characters such as the colon and slashes, producing a string that is no longer a normal URL.
Decoding a URL safely
When debugging, parse the URL first, then inspect its components:
const url = new URL("https://example.com/search?q=red%20%26%20blue");
console.log(url.pathname);
console.log(url.searchParams.get("q"));For an individual encoded component, use decodeURIComponent(). A malformed percent sequence can throw an error, so handle untrusted input deliberately:
function tryDecode(value) {
try {
return decodeURIComponent(value);
} catch {
return null;
}
}Testing from the command line
When sending a request with query data, let the command-line client encode the values instead of manually escaping them. With curl, --get creates a GET request and --data-urlencode encodes each supplied field:
curl --get "https://api.example.test/search" \
--data-urlencode "q=red & blue" \
--data-urlencode "page=2"This is useful for comparing a browser request with an API request. Use verbose output when you need to inspect redirects, request headers, or the final request behavior, but avoid placing secrets or personal data in shell history.
What to double-check
- Full URL or component: Decide whether you are handling a complete URL, a path segment, a query name, or a query value before choosing an encoder.
- Space handling: Percent encoding uses
%20. Form-style query processing may also represent a space with+. Do not assume that every decoder treats plus signs the same way. - Literal plus signs: If a value contains an actual plus sign, encode it as
%2Bwhen using form-style query strings. Otherwise it may be decoded as a space. - Unicode: Verify that the producer and consumer agree on UTF-8 and that the decoded value matches the original text.
- Repeated parameters: Check whether the server expects an array, repeated keys, comma-separated values, or another convention. Do not silently collapse repeated keys into one value.
- Fragments: Text after
#is normally handled by the browser and is not included in the HTTP request sent to the server. Do not use a fragment to transmit data that the server must receive. - HTML embedding: A URL placed in an HTML attribute still needs correct HTML escaping. URL encoding and HTML escaping solve different problems. For related safety guidance, see HTML Escaping and XSS Prevention.
- Redirects and status codes: If a link appears encoded but still fails, inspect the response chain and server behavior. The HTTP status code debugging decision tree can help separate encoding issues from routing or authentication failures.
For a practical debugging record, capture the original input, the constructed URL, the parsed components, the request received by the server, and the decoded values. Redact tokens, credentials, session identifiers, and personal information before sharing logs or using an online URL encoder.
Common mistakes
Encoding the entire URL as one value
Applying component encoding to https://example.com/a?b=c produces an encoded string rather than a navigable URL. Parse the URL and encode only the data being inserted.
Encoding twice
If & becomes %26 and the percent sign is encoded again, the result may become %2526. This is valid text in some workflows but wrong if the receiver expects one decoding pass. Trace each layer and identify where decoding occurs.
Replacing spaces with plus signs everywhere
The plus convention is associated with form-style query encoding, not every URL component. An actual plus sign can be lost if a decoder converts it to a space. Prefer a URL API and test the server’s parser with both values.
Splitting query strings manually
Naive code that splits on every ampersand or equals sign can mishandle encoded delimiters, empty values, repeated keys, and unusual but valid input. Use URLSearchParams or the equivalent parser in your language or framework.
Confusing encoding with security
Percent encoding does not validate input, authorize a user, or prevent injection by itself. Validate values for their intended use, escape them for their output context, and apply authentication and authorization separately. If a URL carries a token, inspect it carefully and avoid pasting it into third-party tools; the JWT inspection guide covers safer handling of token-like data.
When to revisit
Return to this checklist whenever a link is generated by a new library, framework, proxy, API client, or deployment layer. Recheck it before a seasonal planning cycle if campaign links, reporting parameters, redirects, or tracking conventions are being changed. Also revisit the workflow when a browser runtime, server framework, URL parser, or API contract changes.
Before closing a URL-related bug, run this short final checklist:
- Identify the exact component that was encoded or decoded.
- Compare the original value with the parsed value, including spaces, plus signs, Unicode, and delimiters.
- Test empty values, repeated parameters, and values containing
&,=,?,#, and+. - Check one browser request and one command-line request when the issue involves an API.
- Inspect redirects and server logs without exposing secrets.
- Record the expected encoding convention in the API or component documentation.
For adjacent data-formatting work, pair this reference with the JSON formatter and validator guide or the Base64 encoding and decoding guide. Keeping URL construction, parsing, escaping, and security as separate steps makes broken links easier to reproduce and safer to fix.