URL Encoder / Decoder: How We Fixed Download Links With Spaces and Chinese Filenames
URL Encoder / Decoder: How We Fixed Download Links With Spaces and Chinese Filenames Last month I inherited an internal docs portal at work. Teams in Shanghai and Berlin upload files to it, and the names are exactly wh
URL Encoder / Decoder: How We Fixed Download Links With Spaces and Chinese Filenames
Last month I inherited an internal docs portal at work. Teams in Shanghai and Berlin upload files to it, and the names are exactly what you'd expect: Q3 ๆฅๅ.pdf, meeting notes final v2.pdf, ่ฏๅฎก่ฎฐๅฝ-10ๆ.md. The download links were built the naive way:
const url = `https://files.acme.io/docs/${filename}`;
In Chrome, this worked. Of course it worked. Browsers are forgiving and encode things for you silently. Then the links started traveling: into email digests, a Slack bot, and one legacy reporting service. That's when the phone calls started. In Gmail, everything after the first space in the filename vanished. In the legacy service, Q3 ๆฅๅ.pdf arrived as Q3 รฆยฅรฆล ยฅรฅโล .pdf, which is the kind of garbage that makes people distrust your whole system.
It took me an afternoon to sort out all three failure modes. Here's what I should have done from the start.
Method 1: Python (3.11.1)
Python's urllib.parse module has two functions people mix up constantly, and the official docs explain the difference in one line most of us never read: quote() is for path segments, quote_plus() is for query values.
# Python 3.11.1
from urllib.parse import quote, quote_plus, urlencode
filename = "docs/Q3 ๆฅๅ.pdf"
# For a path segment: slashes survive by default (safe="/")
print(quote(filename))
# docs/Q3%20%E6%8A%A5%E5%91%8A.pdf
# For a query value: slashes get encoded too, spaces become +
print(quote_plus(filename))
# docs%2FQ3+%E6%8A%A5%E5%91%8A.pdf
# For a whole query string, just let urlencode do it
print(urlencode({"name": "Q3 ๆฅๅ.pdf", "dir": "uploads/2026"}))
# name=Q3+%E6%8A%A5%E5%91%8A.pdf&dir=uploads%2F2026
Decoding is symmetric: unquote() for anything URL-ish, unquote_plus() when + might mean space. One nuance worth knowing: unquote("a+b") returns a+b unchanged, while unquote_plus("a+b") gives you a b. I've seen both "bugs" filed in the same sprint for opposite reasons.
My fix for the portal ended up as a one-liner in the link builder: quote(filename). Path context, slashes preserved, everything else encoded. Done.
Method 2: Node.js (22.13.1)
Node gives you two layers, and choosing the wrong one is the whole game. encodeURIComponent is for a single segment. encodeURI is for a complete URL and deliberately leaves reserved characters like ?, &, and = alone.
// Node 22.13.1
const filename = "docs/Q3 ๆฅๅ.pdf";
console.log(encodeURIComponent(filename));
// docs%2FQ3%20%E6%8A%A5%E5%91%8A.pdf
console.log(encodeURI(`https://files.acme.io/${filename}`));
// https://files.acme.io/docs/Q3%20%E6%8A%A5%E5%91%8A.pdf
const params = new URLSearchParams({ name: filename, dir: "uploads/2026" });
console.log(params.toString());
// name=docs%2FQ3+%E6%8A%A5%E5%91%8A.pdf&dir=uploads%2F2026
URLSearchParams is what I use now for query strings, because it handles the +-for-space convention and never lets a stray & through. encodeURI on the full URL was the correct choice for the browser-facing download links, since it keeps the URL structure intact while making the filename safe.
Method 3: When you don't want a REPL open
Honestly, most of the time I need percent-encoding, I'm debugging a single link someone pasted into a ticket. Spinning up a Python session for one string is silly. That's where a free online URL encoder earns its keep: paste the string, see the encoded form, paste the encoded form, see it decoded. It runs entirely in the browser, so nothing internal leaks to a server, and it handles UTF-8 correctly โ which matters more than you'd think, as the mojibake story below shows.
I keep this browser-based percent-encoding tool open in a pinned tab next to the regex tester. Two seconds to confirm a hypothesis beats two minutes of REPL warm-up.
Three pitfalls that cost me real time
1. The apostrophe that survives encodeURIComponent. Per MDN, the characters ! ' ( ) * are not escaped. Usually harmless โ until the link lands inside a single-quoted HTML attribute in an email template:
encodeURIComponent("it's 50% off!");
// "it's%2050%25%20off!" โ the apostrophe is still there
The template rendered href='...it's%20...', the browser treated the apostrophe as the end of the attribute, and half the link became plain text. Now I pass anything heading into an attribute through a second .replace(/'/g, "%27").
2. decodeURI pretending to work. A teammate "cleaned up" my decodeURIComponent call, and the logged query strings looked fine. Then a comparison against a decoded value silently failed:
decodeURI("name=a%26b%3Dc");
// "name=a%26b%3Dc" โ nothing decoded, reserved chars are protected
decodeURIComponent("name=a%26b%3Dc");
// "name=a&b=c"
decodeURI refuses to decode %26 and %3D because they're reserved. Nothing throws, nothing warns. The comparison just fails at 2 a.m. in a job nobody owns.
3. unescape() mojibake in legacy code. The legacy reporting service was using the deprecated unescape(), which treats each byte as a Latin-1 character:
unescape("%E5%AD%A3%20%E5%BA%A6");
// "รฅยญยฃ รฅยบยฆ" โ UTF-8 bytes read as Latin-1
ๅญฃ ๅบฆ went in, gibberish came out. The fix was replacing it with decodeURIComponent, which decodes the UTF-8 byte sequence properly. If you find unescape anywhere in a codebase, treat it as a bug, not a style choice.
None of these produce error messages, by the way. That's what makes percent-encoding bugs so annoying: the URL doesn't fail, it just means something slightly different than you intended.
Quick reference
| Situation | Use | Watch out for |
|---|---|---|
| Path segment (Python) | quote(s) |
slashes stay by default, pass safe="" to encode them |
| Query value (Python) |
quote_plus(s) or urlencode
|
+ means space when decoding with unquote_plus
|
| Path segment (Node) | encodeURIComponent(s) |
! ' ( ) * survive; escape ' manually for attributes |
| Full URL (Node) | encodeURI(url) |
reserved chars (& ? =) are never encoded |
| Query string (Node) | URLSearchParams |
handles + and delimiters for you |
| Decoding (either) |
unquote / decodeURIComponent
|
never decodeURI for query values, never unescape
|
The portal's links have been quiet since the fix. If you're debugging one right now, paste the suspect URL into the URL encoder and decode it first โ half the time the bug is visible in the decoded string before you open an editor.
More free browser-based utilities at utilvy.com.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.