URL Encoding for Developers: Libraries and Patterns
While an online URL Encoder is great for quick tasks, software developers must handle encoding programmatically in their applications. This guide covers how to implement safe percent-encoding across popular programming languages.
JavaScript / Node.js
JavaScript provides built-in globals. As a best practice, prefer the URLSearchParams object for building query strings, as it automatically applies encodeURIComponent to all keys and values.
const params = new URLSearchParams({
search: "black & white shoes",
page: 1
});
const url = `/api/items?${params.toString()}`;
// /api/items?search=black+%26+white+shoes&page=1
Python
In Python, the urllib.parse module is your best friend. Use quote() for paths and urlencode() for dictionaries of query parameters.
import urllib.parse
query = {'search': 'black & white shoes'}
encoded_query = urllib.parse.urlencode(query)
# search=black+%26+white+shoes
PHP
PHP has two primary functions: urlencode() (which encodes spaces as +) and rawurlencode() (which encodes spaces as %20 according to RFC 3986). In modern APIs, rawurlencode() is generally preferred for paths, while http_build_query() handles arrays securely.
Avoiding Double Encoding
One of the most common bugs in backend development is accidentally encoding a string that a library has already encoded, or double-decoding on the receiving end. Always check your framework's documentation to see if it automatically decodes incoming parameters (Express.js and Django, for example, do this automatically).