What is URL Encoding (Percent-Encoding)?
When you type a web address into a browser, the internet relies on a very specific set of allowed characters (known as the US-ASCII character set). If your web address contains data that falls outside of this strict set—such as spaces, emojis, foreign languages, or special punctuation—it must be translated before the server can read it.
URL Encoding, formally known as Percent-Encoding, replaces these unsafe characters with a "%" symbol followed by two hexadecimal digits that represent the character's ASCII value. For example, a space becomes %20, and an exclamation mark becomes %21.
Reserved vs. Unreserved Characters
Not every character needs to be encoded. The internet standard defines two lists:
- Unreserved Characters: Letters (A-Z, a-z), numbers (0-9), hyphens (-), periods (.), underscores (_), and tildes (~). These never need to be encoded.
- Reserved Characters: Characters that have special programmatic meaning in a URL. Examples include the question mark (
?) used to start a query string, the ampersand (&) used to separate parameters, and the slash (/) used to separate paths.
Why APIs Break Without It
A common error for junior developers is passing a raw string containing an ampersand inside an API GET request (e.g., ?company=Johnson & Sons). Because the ampersand is a reserved character, the server assumes a new parameter named "Sons" has started, truncating the company name to just "Johnson". URL Encoding the parameter to Johnson%20%26%20Sons ensures the data reaches the database perfectly intact.