Question

I'm trying to use Indy's TIdHTTP component to send data to a website. The code works perfectly on Windows platform but unfortunately it behaves strangely on Android platform. The problem occurs when I use TIdMultipartFormDataStream to send POST parameters.

On Android platform TIdMultipartFormDataStream behaves strangely and this what happens: Suppose your POST data is myparam=myvalue where "myparam" is parameter name and "myvalue" is the parametervalue.

The parameter values gets changed to {FIRST CHARACTER REMOVED}yvalue{NULL CHARACTER} so the final output will look like this yvalue\x00 where \x00 is a null character.

I can replace TIdMultipartFormDataStream with TStringList and I won't face such issue but I prefer to use TIdMultipartFormDataStream because it enables me to upload files + send POST data at the same time.

Sample code:

procedure HTTPPOST;
var
  HTTP: TIdHTTP;
  POSTData: TIdMultipartFormDataStream;
begin
  HTTP := TIdHTTP.Create(nil);
  POSTData := TIdMultipartFormDataStream.Create;

  try
    POSTData.AddFile('myfile','file.txt'); // works
    POSTData.AddFormField('username', 'user1'); // On Android the value gets changed to ser1\x00 where \x00 = Null character

    HTTP.Post('http://www.example.com', POSTData)

  finally
    POSTData.Free;
  end;
end;

Note: the code was tested using Delphi XE5 and Delphi XE5 Update 1

Was it helpful?

Solution

What you describe sounds like a ZEROBASEDSTRINGS bug, which should not exist in the current SVN version because Indy disables ZEROBASEDSTRINGS globally in all of its units after it kept suffering from lots of ZBS bugs in XE4. So I do suggest you upgrade to the lastest SVN verson. If you are having problems doing so, please update your question with details explaining why

OTHER TIPS

Works..

procedure HTTPPOST;
    var
      HTTP: TIdHTTP;
      POSTData: TIdMultipartFormDataStream;
    begin
      HTTP := TIdHTTP.Create(nil);
      POSTData := TIdMultipartFormDataStream.Create;

      try
        POSTData.AddFile('myfile','file.txt'); // works
        POSTData.AddFormField('username', UTF8Encode('user1'), 'utf-8').ContentTransfer:= '8bit';

        HTTP.Post('http://www.example.com', POSTData)

      finally
        POSTData.Free;
      end;
    end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top