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; begin Result := ReplaceStr(URIEncode(S), '%20', '+'); end;
The URIEncode routine was presented in my earlier post.
A version of URIEncodeQueryString is available in my Delphi Doodlings repo. View the code (see
To test this routine you can generate test results at the The URLEncode and URLDecode Page.