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
- The encoder does not number characters. It numbers UTF-8 bytes. ASCII letters stay one byte and often stay literal because they are unreserved.
- 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.
- 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
- Unreserved characters are letters, digits, hyphen, period, underscore, and tilde. They may appear as themselves.
- Reserved characters such as : / ? # [ ] @ ! $ & ' ( ) + , ; = delimit components. Their meaning depends on whether they are being used as delimiters or as data.
- 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
- encodeURI is for a complete URI. It leaves A–Z a–z 0–9 and ; , / ? : @ & = + $ - _ . ! ~ ' ( ) # so the scheme, path, query, and fragment stay separated.
- 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.
- 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
- 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.
- 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.
- 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
- Internationalized domain names are converted with Punycode (xn--) in the host, not with percent-encoding.
- 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.
- 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.