About
In a URL / Uniform Resource Identifier (URI), you have reserved character that are part of the URL syntax that cannot be used.
For instance:
- the scheme separator :
- or the path separator /
Percent-encoding, also known as Url Encoding maps (ie encode) :
- this reserved characters
- to text that starts with a percent (hence percent encoding)
Example:
Although it is known as URL encoding it is, in fact, used more generally within the main Uniform Resource Identifier (URI) set, which includes both:
It is also used in the preparation of data of the application/x-www-form-urlencoded media type, as is often used in the submission of HTML form data
Purpose
The purpose detailed is:
- protecting characters from being interpreted as special/reserved URL delimiters,
- protecting URLs from being mangled by transmission media with character conversions (like some email systems).
Snippet
let uri = "sftp://hostname\\foo:Pwd#Pwd@host:port";
console.log("The encoded URI is: "+encodeURI(uri));
The # must normaly be encoded as %23