Fix URL Encoding & Decoding Errors
Fix double-encoding bugs, plus-sign vs %20 confusion, partial encoding issues, and Unicode URL failures.
URL encoding bugs are among the most subtle web bugs — they produce requests that almost work, making them hard to diagnose. Here are the five most common URL encoding mistakes and their fixes.
Jump to error
Double-encoding produces %2520 instead of %20
URL parameter arrives server-side as '%20hello' instead of ' hello'
Encoding an already-encoded string encodes the `%` to `%25`. The result is `%2520` (double-encoded space) instead of `%20`.
Step-by-step fix
- 1 Use the URL Decoder tool to check if your string is already encoded.
- 2 If `%25` appears in your string, it was double-encoded.
- 3 Decode once before re-encoding, or skip the extra encoding step.
- 4 In code, only call `encodeURIComponent()` once per value.
encodeURIComponent(encodeURIComponent('hello world')) // 'hello%2520world'
encodeURIComponent('hello world') // 'hello%20world'
Space encoded as + vs %20
Server receives '+' instead of space in URL path segment
`+` encodes spaces only in `application/x-www-form-urlencoded` (HTML form data). In URL path segments and most modern contexts, use `%20`.
Step-by-step fix
- 1 For query string values from HTML forms, `+` is correct (servers handle it).
- 2 For URL path segments, use `encodeURIComponent()` which produces `%20`.
- 3 Use `new URLSearchParams()` for query strings — it handles encoding automatically.
// Wrong for URL path: const path = '/search/' + str.replace(/ /g, '+');
// Correct for URL path: const path = '/search/' + encodeURIComponent(str);
Entire URL encoded instead of just the parameter
404 Not Found — URL path slashes (%2F) are encoded
`encodeURIComponent()` encodes everything including `/`, `?`, `&`, `=`. Use it only for parameter values, not entire URLs.
Step-by-step fix
- 1 Identify which parts are parameters vs URL structure.
- 2 Encode only the parameter values with `encodeURIComponent()`.
- 3 Use `new URL()` to construct URLs safely.
const url = encodeURIComponent('https://api.example.com/users?name=Alice');
const url = new URL('https://api.example.com/users');
url.searchParams.set('name', 'Alice');
// url.toString() → correct, encoded URL
Non-ASCII characters in URLs
400 Bad Request — invalid characters in URL
URLs must be ASCII. Non-ASCII characters (accented letters, CJK, emoji) must be percent-encoded using their UTF-8 byte representation.
Step-by-step fix
- 1 Use the URL Encoder tool — it handles UTF-8 to percent-encoding automatically.
- 2 In code, `encodeURIComponent()` handles Unicode correctly.
- 3 For domain names, use Punycode (handled by the browser automatically for `new URL()`).
fetch('https://api.example.com/search?q=café')
fetch(`https://api.example.com/search?q=${encodeURIComponent('café')}`) // 'caf%C3%A9'
Frequently Asked Questions
What is the difference between encodeURI() and encodeURIComponent()?
`encodeURI()` encodes an entire URL but preserves structural characters like `/`, `?`, `=`, `&`. `encodeURIComponent()` encodes everything including those characters — use it for individual parameter values.
Why does my API return 400 for special characters in query params?
The parameter value is likely not encoded or is double-encoded. Use the URL Encoder to check the exact bytes being sent. `new URLSearchParams()` is the safest way to build query strings.
Related Tools
Try the URL Encoder now
Free, runs in your browser, no signup required. Learn more about URL Encoder.
Open URL Encoder →