URL Encoding Explained: encodeURIComponent vs encodeURI
URLs may only contain a limited set of characters. Spaces, &, ?, #, accented letters and emoji have to be percent-encoded: a space becomes %20, é becomes %C3%A9. Getting this wrong causes broken links, missing query parameters and “URI malformed” errors. Here is what to use in JavaScript and Python, with real outputs.
How percent-encoding works
Each unsafe character is converted to its UTF-8 bytes, and each byte is written as % plus two hex digits. é is two bytes in UTF-8 (C3 A9), so it becomes %C3%A9. Letters, digits and - _ . ~ never need encoding.
Some characters are reserved because they structure the URL: / separates path segments, ? starts the query, & and = separate parameters, and # starts the fragment. Inside a value they must be encoded, or they change the meaning of the URL.
JavaScript: encodeURIComponent vs encodeURI
const s = 'a b&c=d/é?#+'
encodeURIComponent(s) // 'a%20b%26c%3Dd%2F%C3%A9%3F%23%2B' encodes everything
encodeURI(s) // 'a%20b&c=d/%C3%A9?#+' keeps & = / ? # +encodeURIComponent– for one piece: a query value, a path segment. This is the one you want 95% of the time.encodeURI– for a whole URL that is already put together. It leaves the structure characters alone:encodeURI('https://x.com/a b?q=é&r=1#top') // 'https://x.com/a%20b?q=%C3%A9&r=1#top'escape()is deprecated and doesn’t use UTF-8 (é→%E9). Don’t use it.
`https://x.com/?q=${name}` with name = "Tom & Jerry" gives ?q=Tom & Jerry – the server sees q=Tom and a second, empty parameter called Jerry.The best way: URL and URLSearchParams
const url = new URL('https://api.example.com/search')
url.searchParams.set('q', 'café & bar')
url.searchParams.set('page', '2')
url.toString()
// 'https://api.example.com/search?q=caf%C3%A9+%26+bar&page=2'
// reading parameters – decoding is automatic
new URL('https://x.com/?q=a%20b&tag=1&tag=2').searchParams.getAll('tag') // ['1', '2']
// a value that goes into the path
`https://x.com/files/${encodeURIComponent('Q1 report/final.pdf')}`
// 'https://x.com/files/Q1%20report%2Ffinal.pdf'URLSearchParams handles encoding, repeated keys and decoding for you. Notice it writes the space as +, not %20 – more on that next.
%20 or + for spaces?
Both are correct, in different places:
%20works everywhere in a URL.+means a space only in the query string (theapplication/x-www-form-urlencodedformat used by HTML forms). In a path,+is a literal plus sign.
decodeURIComponent('a+b') // 'a+b' – doesn't treat + as a space
new URLSearchParams('q=a+b').get('q') // 'a b'So read query strings with URLSearchParams, not decodeURIComponent, or + signs from forms will stay as +. And a literal plus in a value must be sent as %2B – otherwise a phone number like +491234 arrives as 491234.
“URIError: URI malformed”
decodeURIComponent('%E9') // URIError: URI malformed – Latin-1, not UTF-8
decodeURIComponent('100%') // URIError: URI malformed – a lone % signYou get this when the text wasn’t encoded as UTF-8 (often from old systems or escape()), or when it contains a % that isn’t part of an escape. Wrap decoding in try/catch for user input.
Double encoding
encodeURIComponent(encodeURIComponent('a b')) // 'a%2520b'If you see %25 in a URL, something was encoded twice (%25 is the encoded %). It typically happens when you encode a value yourself and then pass it to a library that encodes again – like searchParams.set or requests’s params. Encode exactly once, as late as possible.
Python
from urllib.parse import quote, quote_plus, unquote_plus, urlencode, parse_qs, urlparse
s = 'a b&c=d/é?#+'
quote(s) # 'a%20b%26c%3Dd/%C3%A9%3F%23%2B' ← keeps "/" by default!
quote(s, safe='') # 'a%20b%26c%3Dd%2F%C3%A9%3F%23%2B'
quote_plus(s) # 'a+b%26c%3Dd%2F%C3%A9%3F%23%2B' query-string style
urlencode({'q': 'café & bar', 'page': 2}) # 'q=caf%C3%A9+%26+bar&page=2'
urlencode({'tag': ['a', 'b']}, doseq=True) # 'tag=a&tag=b'
unquote_plus('a+b') # 'a b'
parse_qs(urlparse('https://x.com/?q=a%20b&tag=1&tag=2').query)
# {'q': ['a b'], 'tag': ['1', '2']}quote() leaves / unencoded because it’s meant for paths. When encoding a single value that might contain a slash, pass safe=''.With requests, pass a dict and let it encode: requests.get('https://api.example.com/search', params={'q': 'café & bar', 'tag': ['a', 'b']}) requests …/search?q=caf%C3%A9+%26+bar&tag=a&tag=b.
Quick reference
| Task | JavaScript | Python |
|---|---|---|
| Build a query string | URLSearchParams | urlencode |
| Encode one value | encodeURIComponent | quote(v, safe='') |
| Read query parameters | url.searchParams.get | parse_qs |