URL encoding prevents data from being misread when it travels inside a link. This guide explains when to encode a query parameter, when to encode an entire URL component, how reserved characters behave, and how to test a constructed link before using it in an application or API request.
Overview
A URL is made from different components, including a scheme, host, path, query string, and optional fragment. Each component has its own syntax. URL encoding, also called percent encoding, represents a character as a percent sign followed by two hexadecimal digits. For example, a space can become %20, an ampersand can become %26, and a slash can become %2F.
The important distinction is that you usually encode a value before placing it into a URL; you do not blindly encode a complete URL that already contains its own structure. Encoding the wrong scope can turn separators such as ?, &, and = into data, breaking navigation or changing the meaning of a request.
Reserved and unreserved characters
Some characters are safe to use as ordinary data in many URL components. These are commonly described as unreserved characters:
| Character group | Examples | Typical treatment |
|---|---|---|
| Letters | A-Z, a-z | Usually left unchanged |
| Digits | 0-9 | Usually left unchanged |
| Unreserved punctuation | -, _, ., ~ | Usually left unchanged |
| Query separators | ?, &, = | Structural outside a value; encode inside a value |
| Path separator | / | Structural in a path; encode when it is data in one segment |
| Fragment marker | # | Starts a fragment; encode inside a value |
The practical rule is simple: decide which URL component a value belongs to, then use an encoder designed for that component. A browser-based URL encoder can help inspect a value, but application code should use the URL and query APIs provided by its language or framework whenever possible.
Checklist by scenario
When adding a query parameter
- Start with the raw key and value, such as
search=red apples & pears. - Encode the key and value separately, not the entire query string.
- Join the encoded pairs with
&and separate each key from its value with=. - Inspect the final URL to confirm that separators remain structural.
The intended result is similar to ?search=red%20apples%20%26%20pears. The ampersand in the value is encoded as %26, so it cannot be mistaken for the separator before another parameter.
When a value contains a slash
A slash normally separates path segments. If the slash is part of one value, encode it at the value level. For example, an identifier such as reports/2026 may need to become reports%2F2026 when the server expects one identifier rather than two path segments. Do not encode a slash that you intentionally use to separate path segments.
When a value contains spaces or plus signs
Spaces are a frequent source of confusion. Depending on the API and encoding method, a space may appear as %20 or as +. In application/x-www-form-urlencoded query data, plus commonly represents a space. However, a literal plus sign may need to be encoded as %2B so it is not converted into a space by a decoder. Test both the sender and receiver when a value contains phone numbers, mathematical expressions, search syntax, or other data where plus signs matter.
When constructing a URL in JavaScript
Prefer the platform URL APIs instead of manually concatenating untrusted or complex values:
const url = new URL('https://example.test/search');
url.searchParams.set('q', 'red apples & pears');
url.searchParams.set('page', '2');
console.log(url.toString());This approach keeps query separators separate from parameter data. Similar component-aware APIs exist in other languages and web frameworks. If you use a helper library, check whether it expects raw values or already-encoded values; encoding a value twice can produce results such as %2520.
When debugging an existing link
- Copy the URL as text rather than relying only on what the browser displays.
- Separate the scheme, host, path, query, and fragment.
- Parse the query into individual key-value pairs.
- Decode each value once and compare it with the input your application intended to send.
- Rebuild the URL with a component-aware encoder and compare the result.
What to double-check
Component encoding versus full-URL encoding
Use component encoding for individual values: a query parameter value, a path segment, or a fragment value. Functions commonly called encodeURIComponent are designed for this general purpose in JavaScript. Functions commonly called encodeURI preserve more URL punctuation because they are intended for an already assembled URI. Neither choice replaces careful handling of the URL's structure.
Do not pass a complete URL through a component encoder when you need the browser to recognize its scheme, host, path, and query. Characters such as :, /, ?, and & may be encoded into data, leaving you with a string that no longer behaves as the intended link.
Character encoding
Percent encoding represents bytes, so non-ASCII text should be converted consistently, typically using UTF-8. A value such as café will contain percent-encoded bytes when placed in a URL. Confirm that the receiving service decodes the value using the expected character encoding, especially when troubleshooting multilingual search terms or API payloads.
Already-encoded input
Decide whether your input is raw or encoded before calling an encoder. A raw percent sign in data should generally be encoded as %25. An existing sequence such as %2F may represent a literal encoded slash, but encoding it again produces %252F. Establish one clear ownership rule: either the caller supplies raw values and the URL builder encodes them, or the builder accepts encoded values and documents that contract.
Server-side parsing
Encoding is only half of the process. The server, proxy, router, and application framework may parse paths and queries differently. Check whether a framework decodes before routing, whether repeated parameters are supported, and whether the application treats + as a space. When a request fails, compare the raw request target with the parsed values logged by the application.
Common mistakes
- Encoding the whole URL: This can turn URL syntax into ordinary text. Encode the value or component instead.
- Leaving ampersands in values: An unencoded
&may create an unintended parameter. - Assuming spaces always become plus signs: Use the convention expected by the receiving parser and test it.
- Encoding a deliberate path slash: Only encode the slash when it belongs inside one path value.
- Decoding too early: Decode at a defined boundary, not repeatedly across middleware layers.
- Encoding twice: Watch for percent signs becoming
%25. - Testing only a simple value: Include spaces, ampersands, equals signs, slashes, percent signs, Unicode text, and plus signs in test cases.
- Trusting visual output: Inspect the actual request in browser developer tools, a server log, or an API client.
For sensitive values, avoid pasting tokens, session identifiers, credentials, or private customer data into a public URL encoder. URLs can be copied into history, logs, analytics systems, referrer data, and support tickets. Encoding changes representation; it does not provide secrecy.
When to revisit
Return to this checklist whenever you add a new query parameter, change routing rules, integrate a new API client, or move request handling between frameworks. It is especially useful before a release that changes search, filtering, redirects, pagination, callback URLs, or identifiers containing punctuation.
Recheck the workflow when a browser, HTTP client, proxy, router, or server framework changes. Also revisit it when a previously simple input begins accepting richer text, international characters, pasted URLs, or user-generated search syntax. Maintain small tests for the characters most likely to expose a bug:
red applesfor spacessales & taxfor ampersandsa=bfor equals signsreports/2026for slashes10+2for plus signs50%for percent signscaféfor non-ASCII text
Before shipping, verify the raw URL, the parsed server values, and the final application behavior. If all three match the intended input, your encoding boundary is probably clear. Keep this practical URL encoding guide near other developer utilities, such as a regex tester for validating input patterns, a JWT decoder for inspecting non-secret claims, or a SQL formatter for reviewing query text without confusing formatting with execution.