URL encoding, Base64, and Base64URL solve different transport problems. This guide gives you a practical checklist for choosing the right method, testing round trips, avoiding corrupted values, and remembering that encoding changes representation—it does not provide encryption or access control.
Overview
Choose an encoding method based on where the value will travel and which characters that destination accepts. URL encoding makes text safe for a particular URL component. Base64 represents arbitrary bytes as text. Base64URL is a URL-friendly Base64 variant commonly used in token formats and other URL-safe payloads.
These methods are not interchangeable. A query parameter needs component-aware URL encoding, while a binary file embedded in a JSON request may need Base64. A JWT uses Base64URL for its header and payload segments, but decoding those segments does not validate the token or prove who created it.
| Method | Typical character set | Size overhead | Decoding behavior | Good use cases |
|---|---|---|---|---|
| Percent-encoding (URL encoding) | Preserves permitted URL characters; encodes bytes as %HH | Variable; depends on the input and URL component | Converts percent-encoded bytes back to text or bytes using the agreed character encoding, usually UTF-8 | Query parameters, path components, and form values |
| Standard Base64 | A-Z a-z 0-9 + /, with optional = padding | About one-third larger for larger inputs, plus padding | Maps groups of four characters back to three bytes | Binary data in text-only fields or explicit API contracts |
| Base64URL | Base64 with - and _ instead of + and /; padding may be retained or omitted by contract | Similar to standard Base64 | Requires the decoder to understand the URL-safe alphabet and any padding convention | JWT segments, URL-safe identifiers, and compact protocol values |
| Raw text or JSON | Defined by the media type and character encoding | Usually no encoding overhead | Parsed according to the receiving format, such as JSON or UTF-8 text | Normal JSON fields, API messages, and human-readable data |
For URL behavior, distinguish a URL component from an entire URL. Encode a parameter value before inserting it into a query string; do not blindly encode the whole URL and expect separators such as ?, &, and = to keep their structural meaning. RFC 3986 describes URI syntax and percent-encoding, while RFC 4648 defines Base64 and Base64URL alphabets. A library's function names and defaults still matter, so check its documentation and test the actual output.
Checklist by scenario
Query parameters and path values
- Identify the component: query value, path segment, fragment, or complete URL.
- Convert the original string to the agreed character encoding, normally UTF-8.
- Use a URL component encoder supplied by your language or HTTP client.
- Let the URL builder add separators and delimiters rather than concatenating untrusted text.
- Decode once at the receiving boundary and verify the resulting value.
For example, the value red & blue/green should not be placed directly into a query string. An encoded query value will represent the space and reserved characters safely, while the URL's own & separators remain structural. A slash may be meaningful in a path, so path-segment encoding and query encoding should not be treated as the same operation.
HTML form data
For application/x-www-form-urlencoded, use the form encoder or browser API rather than a generic URI encoder. Form encoding commonly represents spaces as +, whereas percent-encoding represents a space as %20. The server must use the matching form decoder. For multipart forms, do not manually URL-encode the file body; let the multipart implementation create boundaries and headers.
Cookies
Cookie values have their own syntax and deployment constraints. Start with a simple, well-defined value. If the value can contain separators, quotes, non-ASCII text, or structured JSON, use a cookie library or a documented serialization and encoding scheme. Percent-encoding can make a value transportable, but the server and client must agree on whether decoding occurs once, and at which layer. Avoid putting unnecessary sensitive data in a cookie; encoding does not conceal it.
JSON API payloads
Use ordinary JSON strings for text and structured values. Do not Base64-encode every JSON field merely because the request is sent over HTTP. Base64 is appropriate when the API contract explicitly expects binary content represented as text, such as a small byte sequence or a document field. Document the alphabet, padding policy, maximum size, and whether the decoded bytes are compressed, encrypted, or a particular file format.
When sending a JSON request, set the correct media type, serialize once, and avoid URL-encoding the entire JSON body unless the endpoint specifically expects form data. A JSON string containing percent signs is not automatically a URL-encoded JSON document.
JWT segments
A compact JWT commonly has three dot-separated segments. The header and claims segments are JSON bytes encoded with unpadded Base64URL; the signature is also represented using Base64URL. To inspect a token, split it on dots, restore padding if your decoder requires it, and decode with a Base64URL-aware function. Treat the result as untrusted input until the signature, algorithm policy, issuer, audience, expiry, and other application checks have been validated by a JWT library.
Binary content
If the protocol supports file uploads, use its binary or multipart mechanism when practical. If a text-only field is required, Base64 can carry the bytes, but measure the resulting request size and enforce input limits. For URL transport, use Base64URL only when the receiving protocol explicitly defines it; standard Base64 characters can conflict with URL syntax.
Copy-ready round-trip tests
Use values that expose different failure modes:
plain ASCII: hello-world
spaces and reserved characters: red & blue/green? yes
Unicode: café / 東京 / 🚀
empty string: ""
bytes: 00 ff 10 80
For every test, verify that decode(encode(value)) equals the original value or original bytes. Also test an empty value, a value containing a plus sign, and a value ending in one or more Base64 padding bytes. Compare both the encoded representation and the decoded result when debugging an API.
What to double-check
- Character encoding: Confirm whether the input is text, UTF-8 bytes, or another byte representation. Encoding text after an unintended character conversion cannot be repaired by decoding.
- Component rules: Query values, path segments, form bodies, cookie values, and fragments have different grammar and escaping rules.
- Alphabet: Confirm whether the endpoint expects standard Base64 or Base64URL. Check whether
=padding is required, optional, or forbidden. - Decode count: A value encoded once should normally be decoded once. Double-decoding can turn literal text such as
%2Finto a slash unexpectedly. - Type and length: Record whether the decoded result is text, raw bytes, JSON, a compressed stream, or a file. Apply size limits before expensive decoding.
- Canonical form: If values are signed or compared, use the exact serialization and encoding rules required by the protocol. Semantically equivalent JSON or URL values can still produce different signatures.
- Tool behavior: A browser-based URL encode online tool, Base64 encoder decoder, HTTP client, and server framework may use different defaults. Check the generated request in the network inspector or with a reproducible
curlcommand.
For API investigations, pair the encoding check with the endpoint contract. An OpenAPI validator can help identify whether the declared parameter location and schema match the request you are sending. When the problem is in the request itself, a repeatable curl API test makes it easier to compare raw headers, query strings, bodies, and responses.
Common mistakes
- Using Base64 as encryption: Base64 is reversible and offers no confidentiality. Use authenticated encryption through an established library when secrecy and tamper resistance are required.
- Encoding the whole URL: This often converts structural delimiters into data. Encode values at the point where they are added to a URL.
- Confusing
+with a space: Form decoders may interpret plus as a space, while a literal plus in other contexts may need percent-encoding. Test both cases. - Mixing Base64 alphabets: Replacing characters by hand without an agreed padding rule creates tokens that another decoder may reject.
- Decoding JWT content as validation: Anyone can decode a readable token segment. Decoding reveals content; verification establishes whether the token is acceptable.
- Applying URL encoding inside JSON without a contract: This creates values that look corrupted to a server expecting ordinary JSON.
- Ignoring size growth: Base64 increases payload size, and percent-encoding can expand non-ASCII or reserved characters substantially. Check gateway, server, browser, and storage limits.
If an API returns a 4xx response after an encoding change, compare the raw request with the documented contract before changing several layers at once. For broader diagnosis, the HTTP status code debugging guide provides a useful way to separate syntax, validation, authentication, and server-side failures.
When to revisit
Revisit this checklist whenever an API changes its parameter location, media type, token format, cookie serialization, or binary transport. It is also worth reviewing before a release that changes HTTP clients, server frameworks, browser handling, signing code, or language-library versions. A previously working integration can fail when one side begins requiring Base64URL padding, changes form decoding, or applies an additional decode step.
Make the review practical:
- Save representative fixtures containing reserved characters, Unicode, empty values, literal plus signs, and binary bytes.
- Record the expected wire representation and decoded type for each fixture.
- Add round-trip tests at both the producer and consumer boundaries.
- Capture one raw request and response in a safe test environment, removing credentials and personal data.
- Recheck limits, signature inputs, and security assumptions whenever the transport or token format changes.
For related troubleshooting, compare this checklist with the webhook testing guide when signed payloads or retries are involved, and use the browser storage debugging guide when cookie behavior differs between environments. The durable rule is simple: choose URL encoding for URL structure, Base64 for byte-to-text representation, Base64URL for protocols that specify its alphabet, and encryption when the requirement is confidentiality.