idhttp indy post, do request (parameters) with utf-8

none picture none · Nov 10, 2013 · Viewed 34.7k times · Source

I'm trying to post a request, in utf-8 however the server is getting it in Ascii.

Tried the TstringList format of post.

Tried the stream format

Tried forcing the TStringStream to have UTF8 encoding.

Tried updating indy to the xe5 indy

Here is an example code:

var
  server:TIdHttp;
  Parameters,response:TStringStream;
begin
  response := TStringStream.Create;
  Parameters := TStringSTream.create(UTF8String('param1=Value1&param2=عربي/عرب&param3=Value3'),TEncoding.UTF8);
  Server.Post(TIdURI.URLEncode('http://www.example.com/page.php'),Parameters,response);
end;

Now the Arab coding is passed as Ascii in network sniffer.

0060 d8 b9 d8 b1 d8 a8 d9 8a 2f d8 b9 d8 b1 d8 a8 26 ........ /......&

How can I force Indy Http id to pass the request parameters in Utf-8 and not in Ascii?

Answer

Remy Lebeau picture Remy Lebeau · Nov 10, 2013

TStringStream in D2009+ uses UnicodeString and is TEncoding-aware so DO NOT create a UTF8String manually:

var
  server: TIdHttp;
  Parameters,response: TStringStream;
begin
  response := TStringStream.Create;
  Parameters := TStringStream.Create('param1=Value1&param2=عربي/عرب&param3=Value3', TEncoding.UTF8);
  Server.Post('http://www.example.com/page.php',Parameters,response);
end;

Alternatively, the TStrings version also encodes to UTF-8 by default:

var
  server: TIdHttp;
  Parameters: TStringList;
  Response: TStringStream;
begin
  response := TStringStream.Create;
  Parameters := TStringList.Create;
  Parameters.Add('param1=Value1');
  Parameters.Add('param2=عربي/عرب');
  Parameters.Add('param3=Value3');
  Server.Post('http://www.example.com/page.php',Parameters,response);
end;

Either way, you should set the request charset before calling Post() so the server knows you are sending UTF-8 encoded data:

Server.Request.ContentType := 'application/x-www-form-urlencoded';
Server.Request.Charset := 'utf-8';