URIError: URI malformed is one of the most common JavaScript errors in code that reads URLs. It is thrown by decodeURIComponent and decodeURI, and it usually appears in production rather than in testing, because it depends on the data a user sends. This guide lists every cause with a real example, shows how to find the bad sequence, and gives fixes you can copy.
Every result and error on this page was produced by running the code when the page was built.
The error in each browser
It is the same error everywhere, but the message differs by JavaScript engine, according to MDN:
- Chrome, Edge and Node.js (V8):
URIError: URI malformed - Firefox:
URIError: malformed URI sequence - Safari:
URIError: String contained an illegal UTF-16 sequence.
If you search your logs, search for URIError, which all three share.
Why decodeURIComponent is strict
A percent-encoded string is a sequence of bytes written as %XX. decodeURIComponent turns each run of bytes back into characters using UTF-8, and it follows the ECMAScript specification, which says to throw a URIError rather than guess when the input is not valid. There are two ways input can be invalid: the % syntax is wrong, or the bytes are not UTF-8.
Every cause, with a real example
| Input | Cause | decodeURIComponent result |
|---|---|---|
| 100% | A literal percent sign. A % must start a two-digit hex escape. A real percent sign has to be written %25. | URIError: URI malformed |
| %Z1 | Non-hex digits after %. Only 0-9 and A-F are allowed after the percent sign. | URIError: URI malformed |
| % | A % at the very end. Often a value cut off by a length limit, or a string built by concatenation. | URIError: URI malformed |
| caf%E9 | Text encoded as Latin-1 or Windows-1252. %E9 is é in Latin-1, but on its own it is not valid UTF-8. Old systems and escape() produce this. | URIError: URI malformed |
| %E2%82 | A truncated UTF-8 sequence. € is %E2%82%AC. Cutting the URL in the middle of the sequence leaves bytes that mean nothing. | URIError: URI malformed |
| %C3%28 | An invalid UTF-8 continuation byte. %C3 promises a second byte between %80 and %BF; %28 is a plain (. | URIError: URI malformed |
| %u20AC | A %uXXXX escape. The non-standard format produced by escape(). It is not percent-encoding. | URIError: URI malformed |
For comparison, valid input decodes without trouble: decodeURIComponent("caf%C3%A9") returns "café" and decodeURIComponent("100%25") returns "100%".
The cause people miss: decoding twice
A correctly encoded 100%25 decodes to 100%. If a framework already decoded the value and your code decodes it again, the second call sees a bare %:
decodeURIComponent(decodeURIComponent("100%25"))
// URIError: URI malformedMost server frameworks hand you route parameters and query values already decoded. If your code calls decodeURIComponent on them again, it works until someone types a percent sign. Decoding twice is also a security risk; see how servers decode URLs.
Encoding can throw it too
encodeURIComponent and encodeURI throw the same error for a lone surrogate: half of a character such as an emoji, usually left behind when a string is cut by length.
encodeURIComponent("😀".slice(0, 1))
// URIError: URI malformedTruncate by code points (Array.from(str).slice(0, n).join("")) instead of by UTF-16 units.
Find the bad sequence
The error does not say where the problem is. For syntax problems, this finds every % that does not start a valid escape:
function strayPercents(s) {
return [...s.matchAll(/%(?![0-9A-Fa-f]{2})/g)].map((m) => m.index);
}
strayPercents("save 100% now %ZZ")
// [8,14]If that returns nothing but decoding still fails, the problem is the bytes: Latin-1 text, a truncated sequence or an invalid one. The quickest way to see which is to paste the string into the URL Decoder. Its error panel points at the exact sequence, explains it, and offers a lenient decode.
Fix 1: correct the source
The best fix is upstream, wherever the URL is built. A value that contains % must have it encoded as %25, and the encoder does that for you:
"/search?q=" + encodeURIComponent("100% cotton")
// "/search?q=100%25%20cotton"If a link is built by string concatenation, an email template or a CMS field, that is where the unencoded % comes from. The encodeURI vs encodeURIComponent guide explains which function to use.
Fix 2: decode safely when you do not control the input
URLs from users, logs and third parties will sometimes be malformed. Never let a bad URL crash a request handler. Catch the error and fall back to decoding only the parts that are valid:
function safeDecode(s) {
try {
return decodeURIComponent(s);
} catch {
return s.replace(/(?:%[0-9A-Fa-f]{2})+/g, (run) => {
const bytes = run.match(/%[0-9A-Fa-f]{2}/g);
let out = "";
let i = 0;
while (i < bytes.length) {
// A UTF-8 character is 1 to 4 bytes: try the longest that decodes.
let n = Math.min(4, bytes.length - i);
for (; n > 0; n--) {
try {
out += decodeURIComponent(bytes.slice(i, i + n).join(""));
break;
} catch {}
}
if (n === 0) {
out += bytes[i]; // not valid UTF-8: keep it visible
n = 1;
}
i += n;
}
return out;
});
}
}Its real output on the problem inputs:
safeDecode("save%20100%%20now")
// "save 100% now"
safeDecode("caf%C3%A9%20%E2%82")
// "café %E2%82"
safeDecode("%Z1%20ok")
// "%Z1 ok"Valid characters are decoded and broken bytes are left exactly as typed, so nothing is silently lost or replaced. You can still see, and log, what was wrong with the input.
Alternatively, URLSearchParams never throws. It replaces bytes it cannot decode with the Unicode replacement character, so new URLSearchParams("q=caf%E9").get("q") returns "caf�". That is fine for display, but the original byte is lost, so prefer the function above when you need to keep the data.
Fix 3: decode legacy text with its real character set
When the input is Latin-1 or Windows-1252, as from older PHP sites, mainframes or JavaScript's deprecated escape(), the bytes are correct, just not UTF-8. Decoding each byte as Latin-1 recovers the text:
function decodeLatin1(s) {
return s.replace(/%([0-9A-Fa-f]{2})/g, (_, h) =>
String.fromCharCode(parseInt(h, 16)),
);
}
decodeLatin1("caf%E9")
// "café"Latin-1 and Windows-1252 differ only in bytes 0x80 to 0x9F, where Windows-1252 has characters such as € (0x80) and curly quotes. For those, read the bytes into a Uint8Array and use new TextDecoder("windows-1252"). The URL Decoder's Charset option does this for you. The UTF-8 guide explains how to recognise which character set you are looking at.
For %u20AC-style escapes, the deprecated unescape() still works: unescape("%u20AC") returns "€". Use it only to read old data, never to write new URLs.
How other languages handle the same input
JavaScript is unusually strict. The same inputs do not raise errors elsewhere; they are passed through or replaced. Python 3.12 and .NET 8 results below come from running them:
- Python:
unquote('100% sure %ZZ')returns the string unchanged, andunquote('caf%E9')returns'caf�', the replacement character. Passencoding='latin-1'to getcafé, orerrors='strict'to raiseUnicodeDecodeErrorinstead. - C#:
Uri.UnescapeDataString("100% sure")returns the string unchanged, andWebUtility.UrlDecode("caf%E9")returns"caf�".
This matters when a JavaScript front end and a back end in another language disagree: the server may accept a URL that the browser code cannot decode, or quietly turn characters into �.
Checklist
- Log the exact string that failed. The cause is almost always visible in it.
- Look for a bare
%: a literal percent sign, a truncated value, or a second decode of an already decoded value. - Look for single high bytes such as
%E9or%FC: Latin-1 text. Decode with the right character set. - Fix the producer if you own it; encode values once, with a component encoder.
- Wrap every decode of untrusted input in
try/catch, with a tolerant fallback.