Search code examples
delphiindyindy10

Indy http post decode


How can I decode & parse a URL so I can use it as POST parameters.

continue=http%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&rm=false&dsh=..

var
 URL:String;
 Data:TStringList;
 MemoryStream:TMemoryStream;
begin
IdHTTP1.Post(URL, Data, MemoryStream);

Solution

  • You can do it all with TIdURI:

    • To decode use TIdURI.URLDecode(...).
    • To parse, pass the decoded URI to TIdURI.Create.

    Putting it together you'd have something like this:

    var
      URI: TIdURI;
    ....
    URI := TIdURI.Create(TIdURI.URLDecode(EncodedURI));
    try
      // Protocol = URI.Protocol
      // Username = URI.Username
      // Password = URI.Password
      // Host = URI.Host
      // Port = URI.Port
      // Path = URI.Path
      // Query = URI.Params
    finally
      URI.Free;
    end;
    

    With acknowledgement to these answers: