You are not logged in.
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
Double escape "\", example:
{"ToAddress":"someaddress.gmail.com", "Attachment":"c:\\photos\\photo1.bmp"}
Esteban
Offline
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
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.
...
// the client's application request body looks like this
// {"ToAddress":"someaddress.gmail.com", "Attachment":"c:\photos\photo1.bmp"}
Offline
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
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
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
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
Yes, the problem comes from the client side.
RestRequest.AddBody methods removes the double escapes.
Offline