When to Use URL Encoding
URL encoding is required whenever you include user input, special characters, or non-ASCII text in a URL. Common scenarios include building API query strings, encoding OAuth redirect URIs, preparing UTM parameters for marketing campaigns, and handling file paths with spaces. Without encoding, characters like &, =, and # break URL structure.
Three Encoding Modes
βComponent (encodeURIComponent): Encodes everything except A-Z, a-z, 0-9, -, _, ., !, ~, *, ', (, ). Use for query parameter values, form data, and any single URL component.
βFull URL (encodeURI): Preserves URL structure characters like :, /, ?, #, &, =. Use when encoding a complete URL while keeping it navigable.
βAll Characters: Percent-encodes every non-alphanumeric character. Maximum encoding for paranoid mode or strict systems.
Code Examples
JavaScript:
encodeURIComponent('hello world') β hello%20worldPython:
urllib.parse.quote('hello world')PHP:
urlencode('hello world') (uses + for spaces) or rawurlencode() (uses %20)Go:
url.QueryEscape("hello world")