Jump to content
John Gambler

TRESTClient encode issue

Recommended Posts

I have a small program(code bellow) to communicate with a rest API, everything works fine. My problem now is how can i fix the response values with diacritic character.

 

Examples:

 

correct string: não - received string: n\u00E3o

correct string: padrão - received string: padr\u00E3o

 

 

My code:

 

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Edit,
  FMX.Controls.Presentation, FMX.StdCtrls, REST.Types, Data.Bind.Components,
  Data.Bind.ObjectScope, REST.Client, FMX.Memo.Types, FMX.ScrollBox, FMX.Memo, System.JSON;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Send(Url : String);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Send(Url : String);
Var myClient : TRESTClient;
    myRequest : TRESTRequest;
    myResponse : TRESTResponse;
    Content : String;
Begin
   myRequest := TRESTRequest.Create(Application);
   myResponse := TRESTResponse.Create(Application);
   myClient := TRESTClient.Create(nil);

   myRequest.Client := myClient;
   myRequest.Response := myResponse;

   Try
      myRequest.AddParameter('Content-Type', 'application/json', TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
      myRequest.AddParameter('Accept', 'application/json', TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
      myRequest.Method := TRESTRequestMethod.rmPOST;
      Content := '{"emptyString": "", "someUuid": "F60EB930-9B1B-11EF-9657-02240D86274B"}';
      myRequest.Params.AddItem('body',Content,TRESTRequestParameterKind.pkREQUESTBODY,[],ctAPPLICATION_JSON);
      myRequest.Resource := Url;

      myRequest.Execute;

      Memo1.Lines.Add('objectProperty1: ' + (myResponse.JSONValue as TJSONObject).GetValue('objectProperty1').ToJSON());
      Memo1.Lines.Add('objectProperty2: ' + (myResponse.JSONValue as TJSONObject).GetValue('objectProperty2').ToJSON());
   Finally
      Resposta.Free;
      myRequest.Free;
      myClient.Free;
   End;
End;

procedure TForm1.Button1Click(Sender: TObject);
begin
   Send('http://localhost:4000/local/');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
   Send('https://mystackurl.com');
end;

End.

 

Share this post


Link to post

This has nothing to do with the TRESTClient itself. You are receiving the response, parsing it into a TJSONObject, and then converting its child data back into a JSON formatted string.  What you are seeing is the JSON encoding.  By default, ToJSON() encodes non-ASCII Unicode characters in '\uXXXX' format:

function TJSONAncestor.ToJSON: string;
begin
  Result := ToJSON([TJSONOutputOption.EncodeBelow32, TJSONOutputOption.EncodeAbove127]);
end;

To avoid that, you will have to directly call the ToJSON() overload that accepts a TJSONOutputOptions parameter so you can omit the EncodeAbove127 (and EncodeBelow32) flag, eg:

(myResponse.JSONValue as TJSONObject).GetValue('objectProperty1').ToJSON([]) // <-- note the brackets!

 

Edited by Remy Lebeau

Share this post


Link to post
13 minutes ago, Uwe Raabe said:

Have you tried using ToString instead of ToJSON?

Or that! ☝️ Which is basically the same as ToJSON() with all options turned off.

Edited by Remy Lebeau

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×