Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

Your URL Has Three Different Spaces, and One of Them Is a Plus Sign

The bug report was one line: "phone number search returns whitespace." The request URL was ?q=+8613800138000. The server read it back as " 8613800138000" — a leading space where the country code should be. Nothing in th

The bug report was one line: "phone number search returns whitespace."

The request URL was ?q=+8613800138000. The server read it back as " 8613800138000" — a leading space where the country code should be. Nothing in the application code was wrong. The URL was.

Here is the part that trips up almost every team: a space has three encodings, and the character + means two different things depending on which component of the URL you are in.

1. + is a space in the query and a literal plus in the path

application/x-www-form-urlencoded is what HTML forms have submitted since the 1990s, and it is still what most server frameworks parse by default. In that grammar, + means space. In the path, it means nothing at all:

new URLSearchParams("q=a+b").get("q");  // "a b"
new URL("https://x.com/a+b").pathname;  // "/a+b"

Same three characters, two readings — and both are correct in their own context. So a literal plus inside a query value has to be sent as %2B, and a space inside a path segment has to be sent as %20. Mixing them up is how +86 becomes 86.

new URLSearchParams("q=a%2Bb").get("q"); // "a+b"

2. encodeURI encodes the wrong half of the string

encodeURI("https://x.com/s?q=a&b=c d");
// "https://x.com/s?q=a&b=c%20d"

encodeURI deliberately leaves ? & = # / alone, because it assumes you are handing it a complete URL. Hand it a value instead and your data silently becomes structure: the & you meant as text just created a second parameter, b=c. The space got encoded, the thing that actually broke the request did not.

Rule of thumb: encodeURIComponent on values, encodeURI only on whole URLs you built yourself.

3. Malformed escapes throw — but not where they entered

decodeURIComponent("%FF");     // URIError: URI malformed
decodeURIComponent("%E4%B8");  // URIError: URI malformed (truncated CJK)
decodeURIComponent("%E9");     // URIError: URI malformed (lone Latin-1 byte)

decodeURIComponent validates the whole string as UTF-8, so one bad sequence discards the good half too. A 500-row batch decode dies on row 3 and you learn nothing about rows 4-500.

The trap is upstream: new URL("https://x.com/a?q=100%").href returns the string untouched and does not throw. URL normalizes what it understands and passes broken escapes straight through, so the failure surfaces at the decoder, several frames from where it was introduced. If you accept URLs from logs, spreadsheets, or chat, validate percent-escapes yourself.

4. Encoding inflates faster than you budget for

"中文查询".length                       // 4
encodeURIComponent("中文查询").length   // 36

Nine characters per CJK character — 3 UTF-8 bytes, each written as %XX. Against the practical 2,000-character URL budget that leaves you about 222 CJK characters before proxies start answering 414. Emoji are worse: 🚀 is 12 characters.

What to do about it

Decode once to see what you actually have, then encode once. Never "fix" a URL by touching only the half that looks broken — that is exactly how %2520 gets born.

I keep a small client-side URL encoder/decoder around for this: it handles %20 and form-encoded +, shows partially- and double-encoded input for what it is, and reports malformed escapes instead of silently dropping them. Nothing is uploaded — it runs entirely in the browser: https://codetoolbox.pro/tools/url-encoder.html

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.