0

In Delphi XE7 it is advised to use TNetEncoding.URL.Encode

So far I have been using a custom routine:

class function THttp.UrlEncode(const S: string; const InQueryString: Boolean): string;
var
  I: Integer;
begin
  Result := EmptyStr;
  for i := 1 to Length(S) do
    case S[i] of
    // The NoConversion set contains characters as specificed in RFC 1738 and
    // should not be modified unless the standard changes.
    'A'..'Z', 'a'..'z', '*', '@', '.', '_', '-', '0'..'9', 
    '$', '!', '''', '(', ')':
       Result := Result + S[i];
    '—': Result := Result + '%E2%80%94';
    ' ' :
      if InQueryString then
        Result := Result + '+'
      else
        Result := Result + '%20';
   else
     Result := Result + '%' + System.SysUtils.IntToHex(Ord(S[i]), 2);
   end;
end;

Using the method above I have been able to manually specify whether the encoded parameter S is a part of the Path or a part of the Query string.

The spaces should be encoded as + if found in the Path and as %20 is part of the Query parameters.

The function above emits properly

Url := 'http://something/?q=' + THttp.UrlEncode('koko jambo', true);
// Url := http://something/?q=koko%20jambo

but the following is returning different value

Url := 'http://something/?q=' + TNetEncoding.URL.Encode('koko jambo;);
// Url := http://something/?q=koko+jambo

Please elaborate in what way TNetEncoding.URL.Encode should be properly used for encoding query parameters containing spaces as %20?

Gad D Lord
  • 6,620
  • 12
  • 60
  • 106

1 Answers1

1

Read the documentation:

System.NetEncoding.TURLEncoding

TURLEncoding only encodes spaces (as plus signs: +) and the following reserved URL encoding characters: ;:&=+,/?%#[].

It is not possible to make TNetEncoding.URL.Encode encode spaces as %20.

Normally, I would suggest Indy's TIdURI class, as it has separate PathEncode() and ParamsEncode() methods, but they both encode spaces as %20 as well, which does not satisfy your "encoded as + if found in the Path" requirement.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • The last paragraph reads a little strangely. "Normally, I would suggest...., but they encode spaces as `%20` as well". Isn't encoding spaces as `%20` exactly what is wanted? Or did you write `%20` by mistake and meant ``+``? – David Heffernan Oct 20 '14 at 08:23
  • @DavidHeffernan: read the OP's requirement: "The spaces should be encoded as + if found in the Path and as %20 is part of the Query parameters." – Remy Lebeau Oct 20 '14 at 19:05
  • Last part of your answer says, " but they both encode spaces as %20 as well". Isn't that what's wanted. Or is the problem that PathEncode() and ParamsEncode() encode spaces as %20 for spaces in both url and query? – David Heffernan Oct 20 '14 at 19:28
  • @DavidHeffernan: The latter. – Remy Lebeau Oct 20 '14 at 19:37
  • Just a note many years after. What you are talking about here is the difference between URL encoding and URI encoding. In JS, URI encoding is done via EncodeURIComponent which has an extended range of characters it encodes. Also, source character encoding must be taken height for (UTF8, 16, asci, misc iso). – Jon Lennart Aasenden Dec 01 '16 at 05:11