Blog
Developer

URL Encoding for APIs: What to Encode and What to Leave Alone

API bugs caused by URLs are usually small but expensive to debug. A space, ampersand, slash, plus sign, or non-English character can change the meaning of a query string if it is not encoded in the right place.

The common mistake is encoding the entire URL instead of encoding individual values. That turns separators like ? and & into literal text, so the server receives a different request than the one you intended.

Encode values, not structure

Treat the URL structure and the parameter values as separate things. The path, question mark, equals signs, and ampersands define the shape of the request. User-provided values are the part that usually needs encoding.

Avoid double encoding

Double encoding happens when a value that already contains percent sequences is encoded again. The result may look valid, but the API receives characters such as %2520 instead of a single encoded space.

Test small examples first

Before wiring a request into production code, build the final URL with one or two difficult values and decode it back. If the decoded values match the original inputs, your encoding boundary is probably correct.

Good URL encoding is mostly about consistency. Pick one layer of the app to encode values, document it, and avoid re-encoding the same data later.

Open URL Encode / Decode →