0

I'm using Indy to do a Post to an SMS service that will send the SMS, but the SMS text ends up on my phone with %20 instead of spaces, here is the code:

url,text:string;
IdHTTP1: TIdHTTP;
IdSSLIOHandlerSocketOpenSSL2: TIdSSLIOHandlerSocketOpenSSL;
 begin    
    IdSSLIOHandlerSocketOpenSSL2 := TIdSSLIOHandlerSocketOpenSSL.Create;
    IdHTTP1 := TIdHTTP.Create;
    IdSSLIOHandlerSocketOpenSSL2.SSLOptions.Method := sslvSSLv23;
    IdHTTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL2;
    IdHTTP1.HandleRedirects := true;
    IdHTTP1.ReadTimeout := 5000;
    param:=TStringList.create;
    param.Clear;
    param.Add('action=create');
    param.Add('token=' + SMSToken);
    param.Add('to=' + Phone);
    param.Add('msg=' + MessageText);
    url:='https://api.tropo.com/1.0/sessions';
    try
       text:=IdHTTP1.Post(url, param);

thanks

Kim HJ
  • 1,183
  • 2
  • 11
  • 37

2 Answers2

3

The TStrings version of TIdHTTP.Post() sends an application/x-www-form-urlencoded request to the server. The posted data is url-encoded by default. The server needs to decode the posted data before processing it. It sounds like the server-side code is not doing that correctly. You can remove the hoForceEncodeParams flag from the TIdHTTP.HTTPOptions property to disable the url-encoding of the posted data, but I would advise you to report the bug to Tropo instead so they can fix their server-side code.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

TIdHTTP itself does not apply quoted-printable encoding to posted data, so the data being posted has to be quoted-printable encoded beforehand.

In Indy 10, you can use the TIdFormDataField.Charset property to specify how strings are converted to bytes, and then use the TIdFormDataField.ContentTransfer property to specify how the bytes are encoded. For the ContentTransfer, you can specify '7bit', '8bit', 'binary', 'quoted-printable', 'base64', or a blank string (which is equivilent to '7bit', but without stating as much in the MIME header).

Set the TIdFormDataField.CharSet property to a charset that matches what your OS is using, and then set the TIdFormDataField.ContentTransfer property to '8bit'.

Alternatively, use the TStream overloaded version of TIdMultipartFormDataStream.AddFormField() instead of the String overloaded version, then you can store data in your input TStream any way you wish and it will be encoded as-is based on the value of the TIdFormDataField.ContentTransfer property. This should remove the %20 you are getting.

Glen Morse
  • 2,437
  • 8
  • 51
  • 102