Uri.encodeFull Doesn't Encode & or /: Use encodeComponent
Dart's Uri.encodeFull leaves &, /, = and ? alone by design. Learn when to use encodeComponent, how to avoid double encoding, and how to verify the result.
GuideUpdated 5 min read
On this page
You encoded a string in Dart and the ampersand, slash or equals sign came out untouched, so the server split your value into extra parameters. Or you typed special characters into Postman and they were not encoded. Both are the same mistake: the tool cannot tell whether a character in a finished URL is data or structure.
Dart's maintainers closed the report "Uri.encodeUrl() doesn't fully encode url" as intended behavior. An old Postman issue from 2017, "Special characters are not encoded", describes a similar surprise in an HTTP client. Users on both trackers ran into the same wall.
Why it happens
A URL has structure: ? starts the query, & separates parameters, = separates a name from a value, / separates path segments, : follows the scheme. Those characters carry meaning, so a function that encodes a whole URL must leave them alone. Otherwise https://example.com/a?b=c would become unusable.
A value inside a URL has no structure of its own. Any & in it is data and must become %26, or it ends the parameter early.
| Cause | Symptom | Fix |
|---|---|---|
Whole-URL function used on a value (encodeFull, encodeURI) | &, /, = pass through; the server sees extra parameters | Use the component function on the value |
| Component function used on a finished URL | https%3A%2F%2F at the start | Encode only the pieces, then join |
| Encoding in two places (your code and the HTTP client) | %2520, %2526 | Encode once, where you assemble the URL |
Literal + in data | Arrives as a space | Encode it as %2B |
| Slash inside one path segment | Server reads two segments | Encode that segment alone so it becomes %2F |
Fix it: encode each piece with encodeComponent
Take a value with characters that mean something in a URL: R&D/Q3 results = 100.
final value = 'R&D/Q3 results = 100';
Uri.encodeFull(value);
// R&D/Q3%20results%20=%20100
Uri.encodeComponent(value);
// R%26D%2FQ3%20results%20%3D%20100encodeFull only escaped the spaces. Append that result to https://example.com/search?q= and the server sees q=R, then a second parameter starting at D/Q3….
- Split the URL into its pieces: each path segment, each query name, each query value.
- Run
Uri.encodeComponenton each piece separately. - Join them with the real
/,?,&and=. - Never run the component function over the finished URL.
JavaScript has the same split. encodeURI is the "full" function and encodeURIComponent is the one for values.
Quick check in a browser console
Open a browser's JavaScript console and run:
encodeURIComponent('R&D/Q3 results = 100')You get R%26D%2FQ3%20results%20%3D%20100. That is a known-good string to compare with what your code or HTTP client produced.
Check the result in Bytepliers URL Encoder
If you would rather not paste URLs into an online encoder, Bytepliers' URL Encoder works offline and follows RFC 3986. It can encode or decode a string and show how a finished URL splits into parts. It cannot change what Uri.encodeFull or your HTTP client does; the fix stays in your code.
- Open URL Encoder (⌘3).
- Leave the mode on Encode and paste only the value, not the whole URL. Compare the output with your code's result.
- To check a finished URL, paste it and switch to Decode.
- Read Components, which lists the URL parts, and the Query table (Name / Value, with a count such as "N parameters").
If you meant one parameter and the table shows two, an & in your value was not encoded. If a decoded value still contains % sequences, it was probably encoded twice.
Space as + is off by default. Turn it on to see form-style encoding, where a space becomes +.
For a quick check without opening the window, copy the URL and press ⌥⌘B (the default Quick Panel shortcut). Auto-detect decodes URLs and percent-encoded text and lists the query parameters. See the Quick Panel doc. For scripts, you can install the bytepliers command from Settings; see the command-line doc.
Postman and other HTTP clients
I have not re-checked how current Postman versions handle this, and the issue dates from 2017, so test your own case. The principle holds for any client: a tool that sends the URL you typed cannot know whether an & in it is data or a separator.
- Encode the value first (console, Bytepliers or your code), then put the encoded value in the URL.
- Don't encode in two places. If your client or library also encodes, you get double encoding.
- Compare the request as actually sent with your encoded string, then paste the sent URL into URL Encoder's Decode mode to see the parameters.
Checklist before you try again
- Each value and path segment is encoded on its own.
- The separators
? & = /are added after encoding, unescaped. - Only one layer (your code or the client) does the encoding.
- A literal
+is written as%2B. - The URL as sent, decoded in URL Encoder, shows the parameter count you expect.
Other symptoms and fixes
- Non-ASCII text arrives garbled. Percent-encoding should be of the text's UTF-8 bytes. Decode the value on the receiving side and see where it changes.
- The server decodes twice. If your code decodes a value the framework already decoded, a literal
%in the data breaks. Remove one decode. - A path segment contains a
/. Encode that segment alone so the slash becomes%2F, then join it to the rest of the path. Some servers still reject%2Fin paths, which is a server setting, not an encoding error.
If nothing works
If the encoded string looks right but the request still fails, the problem is on the receiving side or in a proxy. Contact the API vendor or your server admin, or file an issue with the library's maintainers.
Include the exact encoded URL as sent, the raw value you meant to send, what the server received, and the library and version. A message you can copy:
My value "R&D/Q3 results = 100" is sent as
https://example.com/search?q=R%26D%2FQ3%20results%20%3D%20100
but the server receives q="R" and a second parameter "D/Q3 results = 100".
Library/client and version: <name, version>.
Is the query string decoded or re-encoded anywhere before it reaches the handler?Common questions
Is Uri.encodeFull broken?
No. It is designed for whole URLs, and the Dart issue was closed as intended behavior.
Should I encode the whole URL?
Almost never. Encode the values and segments, then join them.
Should a space be %20 or +?
%20 is valid for a space anywhere in a URL. + is valid only in form-style query strings. When unsure, use %20.
Does this apply outside Dart?
Yes. Most languages offer a whole-URL function and a component function, and the same rule applies: values get the component function.
Bytepliers is part of MacSuite, and you can try it free for 7 days; see the Bytepliers page.