#1 2020-12-17 13:09:02

gseica
Member
Registered: 2019-08-12
Posts: 8

_Jsonfast

Hello,
can any one tell me what's wrong with the code bellow :

TRestServer = class(TSQLRestServerFullMemory)
  protected
...
  public
    constructor Create(const aRootFolder: TFileName; const aRootURI: RawUTF8); reintroduce;
    destructor Destroy; override;
    property RootFolder: TFileName read fRootFolder;
  published
....
    procedure SendMail(Ctxt: TSQLRestServerURIContext);
  end;

...

procedure TRestServer.SendMail(Ctxt: TSQLRestServerURIContext);
var
  Body      : variant;
begin
  // the client's application request body looks like this
// {"ToAddress":"someaddress.gmail.com", "Attachment":"c:\photos\photo1.bmp"}

// the debuger shows  that CTxt.Call.InBody contains the same information that the client application sends
  Body:= _JsonFast(CTxt.Call.InBody);
// after calling _JSonFast I find that Body.Attachment is 'c:photosphoto1.bmp
end;

Can you tell me how I can avoid this ?

Offline

#2 2020-12-17 13:42:22

EMartin
Member
From: Buenos Aires - Argentina
Registered: 2013-01-09
Posts: 337

Re: _Jsonfast

Double escape "\", example:

{"ToAddress":"someaddress.gmail.com", "Attachment":"c:\\photos\\photo1.bmp"}

Esteban

Offline

#3 2020-12-17 14:38:24

gseica
Member
Registered: 2019-08-12
Posts: 8

Re: _Jsonfast

Thank you for your reply.

I'm not sure I got understood.

Of course, double escape is made by the client application, otherwise the rest server wouldn't get the correct information.

Or, may be, do you suggest to double escape the received CTxt.Call.InBody   before calling _JsonFast ?

Offline

#4 2020-12-17 14:47:21

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: _Jsonfast

The double scape must be done by the client.
In other words, in CTxt.Call^.InBody you should already receive with the double scapes.

In your post, you informed that it is arriving without the double scape, so it was suggested to apply the double scape.

gseica wrote:

...
  // the client's application request body looks like this
// {"ToAddress":"someaddress.gmail.com", "Attachment":"c:\photos\photo1.bmp"}

Offline

#5 2020-12-17 16:04:43

gseica
Member
Registered: 2019-08-12
Posts: 8

Re: _Jsonfast

I repeat : the double escape is made by the client application.

The client application request (the client side is based on a Delphi TRestClient) is :
{"ToAddress":"someaddress.gmail.com", "Attachment":"c:\\photos\\photo1.bmp"}

But on the server side  CTxt.Call.InBody is  {"ToAddress":"someaddress.gmail.com", "Attachment":"c:\photos\photo1.bmp"}

Offline

#6 2020-12-17 16:19:16

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,706
Website

Re: _Jsonfast

This is just not possible.
The input  body is passed directly to the method based service without any parsing at all.
There is something wrong about your client side.

Offline

#7 2020-12-17 16:25:29

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: _Jsonfast

Well, as you can see in your first post, the example you provided was missing the double escape. So it was suggested by @EMartin to include. What is right in this case.

If you are sure it is coming with the double escape then I don't know what it can be.

Or are you saying that CTxt.Call.InBody may be removing the double escape at some point?

In any case, I did a test here and it's ok.

See images:

Request sended (with postman)
https://ibb.co/TR3mLXs

Request received (in debug)
https://ibb.co/1Q0wJNq

Offline

#8 2020-12-17 16:31:11

gseica
Member
Registered: 2019-08-12
Posts: 8

Re: _Jsonfast

The solution to solve the problem was to double escape CTxt.Call.InBody before calling _JSonFast.

Any way, there's something that I don't understand : this assignment, Body:= _JsonFast(CTxt.Call.InBody), practically re-parses the received body.
As long as I don't intend to send the result somewhere via internet I don't need this function to make special chars processing.
Or, perhaps, I shouldn't use _jSonFast to put the body into a variant ?

Offline

#9 2020-12-17 16:41:07

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: _Jsonfast

Forcing the addition of the double escape is wrong.

What is the client code to send the request?
There must be something wrong with the code that is not escaping.

Make a test request with the postman or with the Rest Debuger (free tool from Embarcadero).

Converting to variant is necessary if you want easy access to Json values.

Offline

#10 2020-12-17 16:42:29

gseica
Member
Registered: 2019-08-12
Posts: 8

Re: _Jsonfast

Yes, the problem comes from the client side.

RestRequest.AddBody methods removes the double escapes.

Offline

Board footer

Powered by FluxBB