← Back to all posts

Five scenarios where URL encoding matters (with JS / Python)

When a URL contains non-ASCII text or & and =, correct encoding stops parameters from being truncated.

The scenario

A query string contains non-ASCII text, spaces or &, and the server receives garbled data or split parameters — in all likelihood because the URL was never encoded.

The five scenarios

Query parameters, special characters in paths, nested OAuth parameters, form encoding and restoring a URL from a log.

Code

encodeURIComponent('hello&world')
from urllib.parse import quote
quote('hello&world', safe='')

Try it in the URL codec. Mind the difference between encodeURI and encodeURIComponent.