Every time you click a link, submit a form, or make an API request, URL encoding is working silently in the background. When you search for "cafe near me," the space and any accented characters get converted into percent-encoded sequences before the URL is sent to the server. Understanding URL encoding is essential for web developers, API designers, and anyone who works with web addresses.
> Quick tool: Instantly encode or decode any URL with our free [URL Encoder & Decoder](/tools/url-encoder) — no signup required.
What is URL Encoding?
URL encoding (officially called "percent-encoding") is a mechanism for encoding information in a Uniform Resource Identifier (URI) in a way that is unambiguous and safe to transmit. URLs can only contain a limited set of safe characters — alphanumeric characters and a few symbols (-, _, ., ~). Any other character must be encoded.
Encoding works by replacing the character with a percent sign (%) followed by two hexadecimal digits representing the character's ASCII or UTF-8 byte value. For example:
- Space →
%20 @→%40/→%2F- The accented é in "café" →
%C3%A9(two bytes in UTF-8) &→%26
Which Characters Need to Be Encoded?
Advertisement
The URI specification divides characters into three groups:
Reserved characters: These have special syntactic meaning in URLs and must be encoded when they appear as data rather than structure. Examples include colon, slash, question mark, hash, brackets, at sign, and ampersand.
Unreserved characters: These are safe in any context and do not need encoding: A–Z, a–z, 0–9, hyphen, underscore, period, and tilde.
Other characters: Everything else — spaces, accented characters, non-Latin scripts, control characters — must be percent-encoded.
URL Encoding vs. Query String Encoding
There is a subtle but important difference between general percent-encoding and application/x-www-form-urlencoded encoding (the format used by HTML form submissions).
- In standard percent-encoding, spaces become
%20. - In form-urlencoded encoding (used in HTML forms), spaces are encoded as
+.
The encodeURIComponent() function in JavaScript uses %20, while form submissions use +. This is a common source of bugs. When manually constructing query strings, always use encodeURIComponent() rather than encodeURI().
JavaScript URL Encoding Functions
JavaScript provides three relevant functions:
`encodeURIComponent(value)` — encodes a value to be safely embedded in a URL component (query param value, path segment). Use this 90% of the time.
`encodeURI(uri)` — encodes a complete URL but leaves structural characters unencoded.
`decodeURIComponent(encoded)` — decodes a percent-encoded string back to its original form.
const query = 'cafe latte & pastry' const encoded = encodeURIComponent(query) // Result: 'cafe%20latte%20%26%20pastry' const url = 'https://example.com/search?q=' + encoded
Common URL Encoding Pitfalls
Double encoding: If you call encodeURIComponent on an already-encoded string, you encode the percent signs too — %20 becomes %2520. Always decode before re-encoding.
Inconsistent space handling: Some systems expect + for spaces in query strings, others expect %20. Know which your API uses. Our [URL Encoder](/tools/url-encoder) lets you choose.
Non-ASCII characters: URLs technically only support ASCII, but internationalized URLs use UTF-8 percent-encoding for paths and queries.
Forgetting to encode path parameters: Path parameters that contain slash, question mark, or hash characters must be encoded, or they will be interpreted as URL structure rather than data.
URL Encoding in APIs
When building or consuming REST APIs, URL encoding is critical for query parameters. HTTP libraries like Axios, fetch, and HTTPie handle encoding automatically if you pass parameters as objects:
// Let the library handle encoding — recommended approach
const response = await fetch('/api/search?' + new URLSearchParams({
q: 'C++ programming',
sort: 'price:asc'
}))
// Produces: /api/search?q=C%2B%2B+programming&sort=price%3AascThis is the recommended approach as it eliminates encoding errors. For more complex data that needs to be transmitted, consider [JSON formatting](/tools/json-formatter) as an alternative to URL encoding.
Decoding URLs for Debugging
When debugging URL issues — perhaps an API request is failing or a redirect is going wrong — quickly decoding percent-encoded URLs is invaluable. A URL like https://example.com/search?q=caf%C3%A9%20latte is much clearer when decoded.
[ToolHub's URL Encoder/Decoder](/tools/url-encoder) lets you instantly encode or decode any URL or component, helping you debug issues and understand what URLs actually contain.
Internationalised URLs (IRI)
Internationalized Resource Identifiers (IRIs) extend URIs to allow Unicode characters directly in paths and queries, making URLs human-readable in non-Latin scripts. Browsers display IRIs in decoded form in the address bar, while the actual network request uses percent-encoded UTF-8 bytes.
If you are generating URL slugs for multilingual content, our [Slug Generator](/tools/slug-generator) creates clean, URL-safe slugs from any language.
Frequently Asked Questions
Q: What is the difference between `encodeURI` and `encodeURIComponent`?
encodeURI encodes an entire URL and leaves structural characters (like ?&=) intact. encodeURIComponent encodes a single URL component value — it encodes those structural characters too. Always use encodeURIComponent for parameter values.
Q: Why does a space sometimes appear as `+` and sometimes as `%20` in URLs?
It depends on the encoding context. HTML form submissions use application/x-www-form-urlencoded, which converts spaces to +. Standard percent-encoding (used in paths and modern query building) uses %20. Our [URL Encoder](/tools/url-encoder) lets you choose which output format you need.
Q: How do I decode a URL in Python?
Use urllib.parse.unquote(url) for %xx sequences, or urllib.parse.unquote_plus(url) if + signs should also be decoded as spaces.
Q: Do I need to encode URLs in anchor tags (`<a href>`)?
Modern browsers handle many characters automatically in href attributes, but best practice is to always use properly encoded URLs. Generate clean slugs with our [Slug Generator](/tools/slug-generator) and avoid special characters in path segments.
Conclusion
URL encoding is a foundational web technology that prevents ambiguity and ensures data is transmitted safely through URLs. Understanding the difference between structural URL characters and data characters, using encodeURIComponent correctly in JavaScript, and knowing about the + versus %20 space encoding difference will save you significant debugging time. [Use ToolHub's URL Encoder](/tools/url-encoder) whenever you need to quickly encode or decode URL components.