How does URL encoding (percent-encoding) work?
The RFC 3986 specification allows only a limited set of ASCII characters in URLs (Latin letters, digits and the special characters -_.~). All other characters — including accented and non-Latin letters, spaces, quotes and ampersands — are replaced with a %XX sequence, where XX is the hexadecimal value of each byte in UTF-8.
encodeURIComponent vs encodeURI:
encodeURIComponent escapes every special character, including slashes /, colons : and question marks ?. Use this mode when passing a URL or a parameter value inside another request. encodeURI leaves the structure of the address intact.
Common replacements:
- Space →
%20or+ - Ampersand
&→%26 - Question mark
?→%3F - Accented letter “é” →
%C3%A9