URL Encode & Decode

Percent-encoding (RFC 3986) turns text into UTF-8 bytes and writes each byte as % plus two hex digits, so spaces, non-ASCII, and reserved characters can sit in a path or query. encodeURI leaves structural characters such as slash, question mark, and hash; encodeURIComponent escapes those too, which a single parameter value needs. HTML form bodies (application/x-www-form-urlencoded) often use + for space instead of %20. Encoding a string that already contains %20 produces %2520. A full query versus JSON, and Base64, follow different rules.

What percent-encoding does to bytes inside a URL

Percent-encoding (RFC 3986) is how a URL stays ASCII while still carrying spaces, reserved punctuation, and non-ASCII text. Each character is turned into UTF-8 bytes, and each byte is written as a percent sign plus two hexadecimal digits. The result is reversible: a decoder reads %HH back into the original octet and then into a character. It is not a cipher and it is not Base64.

Three conventions sit next to each other and do not agree on the same characters. encodeURI leaves structural marks such as slash, question mark, and hash so a whole URI can keep its shape. encodeURIComponent escapes those marks too, which is what a single query value or path segment needs. HTML form bodies using application/x-www-form-urlencoded often write a space as a plus sign instead of %20, and they decode a plus back to a space.

A query string that should become JSON is a different job from percent-encoding one value; use the JSON and GET query converter when the text is already a full query or a flat object. Bytes that must travel as a 64-character alphabet belong in Base64, not in percent-encoding.

How UTF-8 bytes, reserved characters, and plus signs line up

Why one character can become several percent sequences

  1. The encoder does not number characters. It numbers UTF-8 bytes. ASCII letters stay one byte and often stay literal because they are unreserved.
  2. A space is byte 0x20, so the percent form is %20. A character outside ASCII, such as a CJK ideograph, is two or three UTF-8 bytes, so the URL shows two or three %HH groups in a row.
  3. Decoding applies the inverse: collect each %HH into a byte, then decode the byte string as UTF-8. If the original bytes were not UTF-8, the characters will not come back even when every percent is well formed.

Which characters RFC 3986 treats as reserved or unreserved

  1. Unreserved characters are letters, digits, hyphen, period, underscore, and tilde. They may appear as themselves.
  2. Reserved characters such as : / ? # [ ] @ ! $ & ' ( ) + , ; = delimit components. Their meaning depends on whether they are being used as delimiters or as data.
  3. When a reserved character is data, it must be percent-encoded. When it is a delimiter, encoding it changes the URI structure: encoding the question mark before the query glues the query onto the path.

How encodeURI, encodeURIComponent, and form plus signs differ

  1. encodeURI is for a complete URI. It leaves A–Z a–z 0–9 and ; , / ? : @ & = + $ - _ . ! ~ ' ( ) # so the scheme, path, query, and fragment stay separated.
  2. encodeURIComponent is for one component. It also escapes slash, question mark, ampersand, equals, plus, and hash, so a value cannot break out of its parameter.
  3. application/x-www-form-urlencoded is a third rule used by HTML forms: space becomes +, and + in the body means space. encodeURIComponent never emits + for a space; it emits %20.

What double encoding and a broken percent sequence do

  1. If %20 is encoded again, the percent sign itself becomes %25, so the text shows %2520. A single decode yields %20, not a space. A second decode yields the space.
  2. A decoder that follows decodeURIComponent rejects a trailing percent, a percent not followed by two hex digits, and an incomplete UTF-8 sequence, typically as a URIError.
  3. Proxies, redirects, and template layers each encode once. Seeing %25 in a log usually means one layer encoded a string that was already encoded.

Why a Chinese domain and a Chinese path are not the same problem

  1. Internationalized domain names are converted with Punycode (xn--) in the host, not with percent-encoding.
  2. A path or query that contains the same characters uses UTF-8 percent-encoding. Browsers display the characters and send the percent form on the wire.
  3. Mojibake appears when one side encoded as UTF-8 and the other decoded as a legacy charset, or when a percent sequence was decoded too many times.

Where percent-encoding shows up outside a single string

Query strings that mix plus, %20, and raw spaces

A raw space is not legal in a URL and may be stripped or rejected by a parser. %20 is the RFC 3986 form of that byte. A plus is a space only under form-urlencoded rules, which many servers apply to the query. The same three inputs are not interchangeable: a literal plus that is data must be %2B, or a form-style decoder will turn it into a space.

Redirects and gateways that encode a value that is already encoded

A redirect that runs encodeURIComponent on the whole target, including a query that already contains %20, produces %2520. The next hop decodes once and still sees percent signs. The fix is to decode to the raw value once, then encode that value at the boundary that builds the new URI, not to encode the URI text as if it were raw data.

Path segments versus query values that contain a slash or question mark

A slash in a path separates segments. If the slash is part of one segment, it has to be %2F or the router will see an extra segment. A question mark starts the query. encodeURI leaves both characters alone, so it is the wrong function for a single segment or a single parameter. A full query that should be compared as JSON belongs in the JSON and GET query converter, not in another round of percent-encoding.

Common questions

What is URL encoding?
URL encoding, more precisely percent-encoding, writes bytes that are not safe as literal URL characters as % followed by two hex digits. Text is converted to UTF-8 first, so one visible character can become several %HH groups. Anyone who knows the rules can reverse it; it does not hide the value.
What is the difference between encodeURI and encodeURIComponent?
encodeURI assumes the input is already a URI and leaves delimiters such as /, ?, &, =, and # in place. encodeURIComponent assumes the input is one component and escapes those delimiters so they cannot change the structure. Using encodeURI on a query value that contains & or = will split the parameter. Using encodeURIComponent on a full URL will escape the slashes and the question mark and break the URL.
Why is a space sometimes a plus and sometimes %20?
RFC 3986 percent-encoding uses %20 for the space byte. The older application/x-www-form-urlencoded media type, used by HTML forms, uses + for space and expects the decoder to turn + back into a space before percent-decoding. encodeURIComponent produces %20. A decoder that also accepts form bodies will treat a bare plus as a space, so a literal plus sign has to be written as %2B.
Why does a Chinese URL turn into percent signs or look garbled?
Chinese characters are not single ASCII bytes. UTF-8 encodes each of them as three bytes, and each byte becomes its own %HH, so a short word becomes a long percent run. That run is the correct on-the-wire form. Garbled characters mean the bytes were decoded with the wrong charset, or the percent text was decoded more than once, or the host name was treated as percent-encoding instead of Punycode.
What does %2520 mean in a URL?
%25 is the encoding of the percent sign itself, and 20 is the two characters that followed it. Together, %2520 is what you get when the text %20 is percent-encoded again. One decode returns the characters %20. A second decode returns a space. If a log shows %2520, some layer encoded a string that was already encoded.
Why does URL decode fail with URI malformed?
decodeURIComponent requires every % to be followed by two hexadecimal digits, and the resulting bytes must be valid UTF-8. A lone %, %2, %ZZ, or a sequence copied from a truncated log will throw. A plus is not a percent sequence; under form rules it is a space and should be turned into a space before that decoder runs. Fix the broken sequence rather than stripping percents at random.
Should a plus sign in a URL be decoded as a space?
Only when the string follows application/x-www-form-urlencoded, which is the usual rule for HTML form bodies and for many query parsers. In a path, or in a value produced by encodeURIComponent, a plus is either a literal plus or the encoding of something else, and a space would have been %20. Decoding every plus as a space will corrupt a literal plus that was not written as %2B.
How is URL encoding different from Base64?
Percent-encoding is for putting bytes into a URI without breaking its delimiters. Base64 is a 64-character alphabet that turns any byte string into ASCII text for JSON, mail, or certificates. They use different tables, different padding, and different places. A query that is really structured data is easier to read after the JSON and GET query converter; a binary blob belongs in Base64.