URL Encoding and Decoding Explained: A Reference for Query Strings, Paths, and APIs
URLsURL encodingJavaScriptAPIsHTTPdebugging

URL Encoding and Decoding Explained: A Reference for Query Strings, Paths, and APIs

FFlorence Cloud Editorial Team
2026-12-02
7 min read

Learn when to encode query values, paths, fragments, and form data, with JavaScript, shell, and debugging guidance.

URL encoding becomes much easier to debug when you separate the URL itself from the values placed inside it. This reference explains percent encoding, reserved characters, query parameters, path segments, fragments, form data, and Unicode, then shows how to choose the right JavaScript API and verify requests from a shell or application log.

Overview

A URL has structure. Its scheme, host, path, query, and fragment each use certain characters as delimiters. For example, in https://example.test/search?q=red%20shoes, the question mark begins the query, the equals sign separates the parameter name from its value, and %20 represents a space in the value.

URL encoding, more precisely percent encoding, represents a character or byte with a percent sign followed by two hexadecimal digits. It allows data to travel through a URL without being mistaken for URL syntax. Encoding is not encryption: anyone who can see the URL can usually decode it.

The most important rule is to encode a value for the context where it will be inserted. Encode a query parameter value separately from the complete URL. Encode a path segment separately from the path. Avoid taking a fully assembled URL and encoding it again, because that can turn separators such as ? and & into data.

When reviewing a failing request, ask three questions:

  • Which part of the URL is changing: a path, query parameter, fragment, or form body?
  • Was the value encoded once, more than once, or not at all?
  • Is the display showing the raw URL, a decoded value, or a value serialized by a framework?

What to track

Reserved and unreserved characters

Unreserved characters can generally appear without percent encoding:

CategoryCharactersPractical note
Letters and digitsA-Z a-z 0-9Usually safe in URL components.
Unreserved punctuation- . _ ~These characters normally do not need encoding.
Common delimiters/ ? # & =Meaning depends on the URL component; encode them when they belong to data.
Space%20 or +%20 is percent encoding; + is commonly used by form-style query serialization.

A slash in a path can separate two segments, while a slash inside a product name may be part of the product name. If the value is reports/2026 and should remain one segment, encode the slash. If it represents two path segments, construct those segments separately.

Query parameters

Query parameters are the most common place to encode user input: searches, filters, redirect targets, and API options. Encode each name and value as data rather than manually joining strings.

const url = new URL("https://api.example.test/search");
url.searchParams.set("q", "red shoes & boots");
url.searchParams.set("page", "2");

console.log(url.href);
// The URL object serializes the query safely for you.

URLSearchParams is designed for query-style name/value pairs. Its serialization may display a space as +, which is normal for this format. Do not treat the visual difference between + and %20 as proof that the value changed; parse the URL and inspect the decoded parameter value.

Paths and fragments

Use a path-segment operation for a value inserted between slashes. In JavaScript, encodeURIComponent() is often suitable for an individual segment, but it should not be used to encode an entire URL. A fragment follows # and is handled by the browser rather than sent as part of the HTTP request to the server. If a fragment contains application state, encode that state according to the fragment format your application expects.

Unicode and form data

Non-ASCII text is represented through encoded bytes, commonly using UTF-8 before those bytes are written as percent escapes. A value such as café may therefore appear with several percent-encoded bytes rather than a single code. Keep the original Unicode value available for display and comparisons, and let a standards-aware URL or form API serialize it.

Query strings and HTML form submissions can look similar but are not identical in every detail. Form-style serialization commonly uses application/x-www-form-urlencoded, where spaces may become plus signs. Check the request's content type and the receiving framework before deciding that a serialized value is wrong.

Cadence and checkpoints

URL encoding is not a setting that needs daily monitoring, but it deserves a small recurring check wherever URLs are generated, redirected, logged, or signed. Add the following checkpoints to a monthly or quarterly developer-tools review, and repeat them after a framework, runtime, HTTP client, router, or API change.

  1. Record the contract. For each endpoint, note which values are path segments, query parameters, fragments, or request-body fields. State whether the receiver expects decoded text, form encoding, or JSON.
  2. Test boundary characters. Include spaces, ampersands, equals signs, question marks, slashes, percent signs, plus signs, quotation marks, and non-ASCII text. Test an empty value as well.
  3. Compare representations. Inspect the value before serialization, the final URL, the outgoing request, and the server's parsed value. This shows where a change was introduced.
  4. Check for double encoding. A literal percent sign often appears as %25. If a value that should be red%20shoes becomes red%2520shoes, an already encoded value was likely encoded again.
  5. Review logs and redirects. Confirm whether observability tools display raw or decoded URLs. A log viewer may normalize a URL, while a browser address bar or proxy may preserve a different representation.

Keep a small table of expected input, serialized output, and parsed output in automated tests. This is more reliable than checking whether a URL merely “looks right.” For related HTTP troubleshooting, the HTTP Cache-Control guide can help separate URL issues from response-caching behavior.

How to interpret changes

encodeURI versus encodeURIComponent

Use encodeURI() when you have a URI-like string and need to preserve its structural delimiters. It is intended for a complete URI and therefore does not encode characters that commonly separate components.

Use encodeURIComponent() for one component value, such as a query parameter value or a path segment. It encodes characters that could otherwise act as delimiters. This is usually the safer choice when inserting user input into an existing URL structure.

const term = "red shoes & boots";
const url = "/search?q=" + encodeURIComponent(term);

// Do not do this to a complete URL:
// encodeURIComponent("https://example.test/search?q=red%20shoes")

For new code, prefer structured APIs such as URL and URLSearchParams when they fit the task. They make the boundary between URL structure and data visible. Framework helpers can also be appropriate, but verify whether they expect raw values or already encoded strings before using them.

Common debugging patterns

  • The server receives separate parameters unexpectedly: an ampersand in a value was probably not encoded.
  • A route no longer matches: a slash may have been encoded as data, or a complete path may have been encoded as one segment.
  • A plus sign becomes a space: a form-style parser may interpret + as a space. Encode a literal plus according to the serialization format.
  • Text contains percent sequences after decoding: the input may have been encoded twice, or the application may be decoding at the wrong layer.
  • A redirect target is malformed: treat the target as a parameter value, not as a URL fragment to concatenate without encoding.

From a shell, quote the complete URL so the shell does not interpret characters before the HTTP client sees them. For generated query values, an option such as curl --get --data-urlencode 'q=red shoes & boots' https://api.example.test/search communicates that the value should be serialized as a parameter. Always compare the command's resulting request with the application implementation.

Encoding is not input validation, authorization, or sanitization. Validate values against the endpoint's expected type and constraints, and do not use URL encoding as a defense against injection in another output context. A URL that is safe as a query value may still be unsafe when later inserted into HTML, a shell command, or a database statement.

When to revisit

Revisit this reference when a URL starts changing between environments, when a router or HTTP client is upgraded, or when an API adds filters containing spaces, Unicode, brackets, slashes, or punctuation. It is also worth reviewing after a redirect migration, a change in proxy or CDN behavior, or a logging update that alters URL normalization.

Use this practical checklist:

  1. Write down the raw value and the URL component that should contain it.
  2. Construct the URL with URL or a component-specific encoder.
  3. Inspect the serialized URL and confirm that delimiters still describe the intended structure.
  4. Parse the result on the receiving side and compare the decoded value with the original.
  5. Add the case to a regression test, especially if it exposed double encoding or a +/%20 mismatch.

That routine turns URL encoding from a last-minute string-replacement problem into a documented, testable part of the API contract. Recheck the contract on a monthly or quarterly cadence, and sooner whenever the tools that construct, route, proxy, or observe your URLs change.

Related Topics

#URLs#URL encoding#JavaScript#APIs#HTTP#debugging
F

Florence Cloud Editorial Team

Developer Tools Editor

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.