%url-decoder.net

How to Fix URIError: URI Malformed in JavaScript

Updated November 25, 2026 · 8 min read

Short answer

URIError: URI malformed means decodeURIComponent or decodeURI found a % sequence it cannot turn into text: a % not followed by two hex digits (such as a literal 100%), or bytes that are not valid UTF-8 (such as %E9 from a Latin-1 system, or a sequence cut in half). Fix the source by encoding % as %25, catch the error and fall back to a tolerant decode, or decode legacy text with the right character set.

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

InputCausedecodeURIComponent 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
%Z1Non-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%E9Text 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%82A 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%28An invalid UTF-8 continuation byte. %C3 promises a second byte between %80 and %BF; %28 is a plain (.URIError: URI malformed
%u20ACA %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 malformed

Most 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 malformed

Truncate 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, and unquote('caf%E9') returns 'caf�', the replacement character. Pass encoding='latin-1' to get café, or errors='strict' to raise UnicodeDecodeError instead.
  • C#: Uri.UnescapeDataString("100% sure") returns the string unchanged, and WebUtility.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

  1. Log the exact string that failed. The cause is almost always visible in it.
  2. Look for a bare %: a literal percent sign, a truncated value, or a second decode of an already decoded value.
  3. Look for single high bytes such as %E9 or %FC: Latin-1 text. Decode with the right character set.
  4. Fix the producer if you own it; encode values once, with a component encoder.
  5. Wrap every decode of untrusted input in try/catch, with a tolerant fallback.

Questions

What causes URIError: URI malformed?

decodeURIComponent or decodeURI met a percent sequence that is not valid: a % without two hex digits after it, or escapes whose bytes are not valid UTF-8. Encoding can also throw it, when a string contains half of an emoji's surrogate pair.

How do I decode a string with a literal % sign?

If you control the source, encode the percent sign as %25 so 100% is sent as 100%25. If you do not, wrap decodeURIComponent in try/catch and fall back to decoding only the valid escape sequences, leaving the rest as typed.

Why does %E9 throw but %C3%A9 does not?

Both mean é, in different character sets. JavaScript's decoders only accept UTF-8, where é is the two bytes %C3%A9. %E9 is the Latin-1 or Windows-1252 byte, which is not valid UTF-8 on its own.

Is the error message different in Firefox and Safari?

Yes. Chrome, Edge and Node.js say "URI malformed", Firefox says "malformed URI sequence", and Safari says "String contained an illegal UTF-16 sequence." All three are the same URIError.

Does Python or C# throw the same error?

No. Python's unquote leaves an invalid % sequence as it is and replaces invalid UTF-8 with the replacement character. C#'s Uri.UnescapeDataString also leaves invalid sequences unchanged. JavaScript's decoders are the strict ones.

Why do I get URI malformed only on some requests?

Usually because the error depends on the data: a search term with a % sign, a name with an accent sent by an old client, or a link cut short by an email or chat app. Log the raw string that failed to find which case it is.

See also

Try everything in this guide hands-on, free and entirely in your browser.

Find the bad sequence in the URL Decoder

Keep reading