UTF-8 encoded string support in TRESTClient Delphi XE5

I want to send tweets using the new TRest components in Delphi XE5. I'm looking for a UTF8 way to encode my tweet that contains the characters IS0-8859-1. The code below works, but includes codepage conversion, etc. Is the best way? Is anyone

procedure TTwitterApi.Send(Tweet: string);
begin
  Reset;

  // Encode as UTF8 within (UTF-16 Delphi) string
  Tweet := EncodeAsUTF8(Tweet);

  FRestRequest.Resource := '1.1/statuses/update.json';
  FRestRequest.Method := rmPOST;
  FRestRequest.Params.AddItem('status', Tweet, pkGETorPOST);
  FRestRequest.Execute;
end;


function TTwitterApi.EncodeAsUTF8(UnicodeStr: string): string;
var
  UTF8Str: AnsiString;
  TempStr: RawByteString;
begin
  TempStr := UTF8Encode(UnicodeStr);
  SetLength(UTF8Str, Length(TempStr));
  Move(TempStr[1], UTF8Str[1], Length(UTF8Str));
  Result := UTF8Str;
end;
+4
source share
3 answers

The Twitter 1.1/statuses/update.jsonURL expects the data to be encoded in a format application/x-www-form-urlencoded, so you need to set the property TRESTClient.ContentTypeto ctAPPLICATION_X_WWW_FORM_URLENCODED(default is set ctNone).

UTF-8, TRESTClient Indy , Indy , , Embarcadero TRESTClient ( ). , ​​ . UTF-8 ( , BTW), Twitter, UTF-8 ( charset Content-Type REST), TRESTClient , . , TRESTClient REST , , , , , .

, EncodeAsUTF8(). UnicodeString, UTF-8, . UTF-8 AnsiString, UTF-16 UniodeString, Ansi RTL, , UTF-8. :

function TTwitterApi.EncodeAsUTF8(UnicodeStr: string): string;
var
  UTF8Str: UTF8String;
  I: Integer;
begin
  UTF8Str := UTF8String(UnicodeStr);
  SetLength(Result, Length(UTF8Str));
  for I := 1 to Length(UTF8Str) do
    Result[I] := Char(Ord(UTF8Str[I]));
end;

TRESTClient UTF-8 POST-. charset Content-Type ( Twitter UTF-8, charset).

, , , TRESTClient , Indy TIdHTTP ( application/x-www-form-urlencoded TRESTClient), :

procedure TTwitterApi.Send(Tweet: string);
var
  Params: TStringList;
begin
  Reset;

  Params := TStringList.Create;
  try
    FParams.Add('status=' + Tweet);
    FIdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
    FIdHTTP.Request.Charset := 'utf-8';
    FIdHTTP.Post('https://api.twitter.com/1.1/statuses/update.json', Params, IndyTextEncoding_UTF8);
  finally
    Params.Free;
  end;
end;
+5

TRestRequest android, , UTF8, , IdHttp Indy .

0

I solved this problem with another API provider (not Twitter) as follows:

function EncodeAsUTF8(UnicodeStr: string): AnsiString; // <-- Note the Ansi
var
  UTF8Str: UTF8String;
  I: Integer;
begin
  UTF8Str := UTF8String(UnicodeStr);
  SetLength(Result, Length(UTF8Str));
  for I := 1 to Length(UTF8Str) do
    Result[I] := AnsiChar(Ord(UTF8Str[I])); // <-- Note the Ansi
end;

...

fRESTClient1 := TRESTClient.Create(nil);
fRESTClient1.Accept := 'application/json';
fRESTClient1.AcceptCharset := 'UTF-8';
fRESTClient1.AcceptEncoding := 'identity';
fRESTClient1.ContentType := 'application/x-www-form-urlencoded';

...

rrOrder := TRESTRequest.Create(nil);
rrOrder.Accept := 'application/json';
rrOrder.AcceptCharset := 'UTF-8';
rrOrder.Client := fRESTClient1; {}
rrOrder.Method := rmPOST;
rrOrder.Resource := 'xxxxxx';
rrOrder.Params.AddItem('', EncodeAsUTF8(aJson), pkREQUESTBODY, [poDoNotEncode]);

rrOrder.Execute;
0
source

All Articles