URL Encoding revisited

In my previous post I covered URI encoding part of a URI or URL. What I didn't cover was the almost trivial case of encoding a query string. The only difference is that spaces in a query string are converted to reserved '+' characters that are not then percent encoded.

The only complication arises when the query string to be encoded already contains '+' symbols. They must be percent-encoded so they are not confused with the symbols that are used to replace spaces.

In the original version of this post I completely misunderstood this and presented code that percent-encoded both literal and space-replacement '+' symbols.

Unfortunately percent-encoding at this stage also encodes spaces as %20. We fix this by simply replacing each occurence of %20 with a '+' symbol.

Here's the code: it's a very simple function that encodes a query string value passed as a UTF-8 string parameter:

function URIEncodeQueryString(const S: UTF8String): string;
  Result := ReplaceStr(URIEncode(S), '%20', '+');

The URIEncode routine was presented in my earlier post.

A version of URIEncodeQueryString is available in my Delphi Doodlings repo. View the code (see UURIEncode.pas).

To test this routine you can generate test results at the The URLEncode and URLDecode Page.


  1. The original post was just about as wrong as it could be. So I've completely reworked it.


Post a Comment

Comments are very welcome, but please don't comment here if:

1) You have a query about, or a bug report for, one of my programs or libraries. Most of my posts contain a link to the relevant repository where there will be an issue tracker you can use.

2) You have a query about any 3rd party programs I feature, please address them to the program's developer(s) - there will be a link in the post.

3) You're one of the tiny, tiny minority who are aggressive or abusive - in the bin you go!


Popular posts from this blog

Focus On: Deleaker - a Delphi, C++ & C# Memory Leak Detector

Embarcadero Announce RAD Studio 11 Is Coming

Some Features of the Upcoming Delphi 11 (probably)