URL Encode/Decode
About URL Encoding/Decoding
What is a URL
A URL (Uniform Resource Locator) is a string used to locate internet resources, consisting of protocol, domain name, path, etc. Since URLs can only contain the ASCII character set, special characters and non-ASCII characters must be encoded.
What is URL Encoding
URL encoding (percent-encoding) converts special characters to a % followed by two hexadecimal digits. This ensures safe data transmission in URLs by preventing ambiguity with special characters. For example, spaces are encoded as %20 or +.
Python Implementation
from urllib.parse import quote, unquote
# Encoding
encoded = quote("hello world!@", safe="")
print(encoded) # hello%20world%21%40
# Decoding
decoded = unquote("hello%20world%21%40")
print(decoded) # hello world!@
JavaScript Implementation
// Encoding
const encoded = encodeURIComponent('hello world!@');
console.log(encoded); // hello%20world%21%40
// Decoding
const decoded = decodeURIComponent('hello%20world%21%40');
console.log(decoded); // hello world!@
Java Implementation
import java.net.URLEncoder;
import java.net.URLDecoder;
// Encoding
String encoded = URLEncoder.encode("hello world!@", "UTF-8");
System.out.println(encoded); // hello+world%21%40
// Decoding
String decoded = URLDecoder.decode("hello+world%21%40", "UTF-8");
System.out.println(decoded); // hello world!@
Usage Scenarios
- Passing special characters in URL parameters
- Form data submission encoding
- Secure parameter transmission in API requests
- Handling non-ASCII characters (e.g., Chinese)
Important Notes
- Encoding converts all non-safe characters (including alphanumerics and -_.~)
- Invalid encoding during decoding will show error
- Spaces may become %20 or +, consider application context
- Encoded strings increase in length, mind length limits