EncodingAlso known as: URL Encoding, Percent-encoding
URL Encoding (Percent-encoding)
A mechanism for encoding information in a Uniform Resource Identifier (URI) using a percent sign followed by two hexadecimal digits.
URL Encoding, officially known as percent-encoding, is a mechanism for encoding information in a Uniform Resource Identifier (URI). It is used to ensure that characters that have special meaning in a URL (like ?, &, =, #) or characters that are not permitted in URLs (like spaces or non-ASCII characters) are safely transmitted over the internet.
How it works
When a character needs to be URL encoded, it is replaced by a percent sign (%) followed by the two-digit hexadecimal representation of its ASCII (or UTF-8) value.
For example:
- A space (
) becomes%20(or sometimes+in form data) - An exclamation mark (
!) becomes%21 - A question mark (
?) becomes%3F - An ampersand (
&) becomes%26
Reserved vs. Unreserved Characters
The URI specification defines two types of characters:
- Unreserved Characters: Characters that are allowed in a URL and do not need to be encoded. These include uppercase and lowercase letters (
A-Z,a-z), decimal digits (0-9), hyphen (-), period (.), underscore (_), and tilde (~). - Reserved Characters: Characters that have special meaning within the URL syntax (such as acting as delimiters). If data being passed in a URL contains these characters, they must be percent-encoded to distinguish them from their role as delimiters. Reserved characters include
! * ' ( ) ; : @ & = + $ , / ? % # [ ].
Common Use Cases
- Query Parameters: When submitting HTML forms using the GET method, the form data is appended to the URL as a query string. Spaces and special characters in user input must be URL encoded.
- API Requests: REST APIs often accept data via URL parameters or path variables, requiring the client to URL encode the data before sending the request.