#1 2012-12-07 09:20:50

corchi72
Member
Registered: 2010-12-10
Posts: 232

no longer works TSQLRestServerCallBackParams(aParams).SentData!

the new version does not work the following line of code, with which I was sending to the server a very large xml,

client side

if FHttpDatabase.URI(format('%s/%s',[FHttpDatabase.Model.Root,StringtoUTF8('XMLAsData')])+UrlEncode(['filename',StringtoUTF8(sfilename)]),'PUT',@aResponse,nil,@aData).Lo=200 then

Server side

no longer works

function TFileServer.XMLAsData(var aParams: TSQLRestServerCallBackParams): Integer;
var //aData: TSQLRawBlob;
   sfilename:RawUTF8;
   XmlDoc: TNativeXml;
   aData: RawUTF8 ;
   Node: TXmlNode;
begin
  //writeln('Start XMLAsData');
  if not UrlDecodeNeedParameters(aParams.Parameters,'FILENAME') then begin
    result := 404; // invalid Request
   {$ifNdef SERVICE}
    writeln('invalid Request');
    {$ENDIF}
    exit;
  end;
  while aParams.Parameters<>nil do begin
    UrlDecodeValue(aParams.Parameters,'FILENAME=',sfilename,@aParams.Parameters);
  end;
   {$ifNdef SERVICE}
  writeln(format('filename: %s',[sfilename]));
   {$ENDIF}

  if length(aParams.SentData)>0 then
  begin
    {$ifNdef SERVICE}
    if DebugHook<>0 then
     writeln(format('Exist data: %s',[aParams.SentData]));
    {$ENDIF}
     try
        XmlDoc := TNativeXml.Create(nil);
        XmlDoc.ReadFromString(UTF8ToString(aParams.SentData));

        XmlDoc.SaveToFile(sfilename);
    {$ifNdef SERVICE}
        writeln(format('Exist data: %s',[sfilename]));
    {$ENDIF}
     except
      on E: Exception do
      begin
     {$ifNdef SERVICE}
         writeln('Error launching the server' +#10+E.Message);
     {$ENDIF}
         result := 404; // invalid Request
         exit; // we need a valid record and its ID
      end;
     end;
     XmlDoc.Free;
  end
  else
   begin
    result := 404; // invalid Request
    exit; // we need a valid record and its ID
   end;

  aParams.Resp := JSONEncodeResult([sfilename]);

  //aResp := JSONEncodeResult([SynCommons.BinToHex(aData)]);
  // idem: aResp := JSONEncode(['result',BinToHex(aRecord.fData)],TempMemoryStream);
  result := 200; // success
end;

I tried to use the following call but does not work,
now what can I use to send xml filename to the server?

client side

aResponse := FHttpDatabase.CallBackGetResult('XMLAsData',['filename',StringtoUTF8(sfilename),'XML',aData]);

Server

function TFileServer.XMLAsData(var aParams: TSQLRestServerCallBackParams): Integer;
var //aData: TSQLRawBlob;
   sfilename:RawUTF8;
   XmlDoc: TNativeXml;
   aData: RawUTF8 ;
   Node: TXmlNode;
   sXML:RawUTF8;
begin
  //writeln('Start XMLAsData');
  if not UrlDecodeNeedParameters(aParams.Parameters,'FILENAME') then begin
    result := 404; // invalid Request
   {$ifNdef SERVICE}
    writeln('invalid Request');
    {$ENDIF}
    exit;
  end;
  if not UrlDecodeNeedParameters(aParams.Parameters,'XML') then begin
    result := 404; // invalid Request
   {$ifNdef SERVICE}
    writeln('invalid Request');
    {$ENDIF}
    exit;
  end;

  while aParams.Parameters<>nil do begin
    UrlDecodeValue(aParams.Parameters,'FILENAME=',sfilename,@aParams.Parameters);
    UrlDecodeValue(aParams.Parameters,'XML=',sXML,@aParams.Parameters);
  end;
   {$ifNdef SERVICE}
  writeln(format('filename: %s',[sfilename]));
   {$ENDIF}


  if length(sXML)>0 then
  begin
    {$ifNdef SERVICE}
    if DebugHook<>0 then
     writeln(format('Exist data: %s',[sXML]));
    {$ENDIF}
     try
        XmlDoc := TNativeXml.Create(nil);
        XmlDoc.ReadFromString(UTF8ToString(sXML));

        XmlDoc.SaveToFile(sfilename);
    {$ifNdef SERVICE}
        writeln(format('Exist data: %s',[sfilename]));
    {$ENDIF}
     except
      on E: Exception do
      begin
     {$ifNdef SERVICE}
         writeln('Error launching the server' +#10+E.Message);
     {$ENDIF}
         result := 404; // invalid Request
         exit; // we need a valid record and its ID
      end;
     end;
     XmlDoc.Free;
  end
  else
   begin
    result := 404; // invalid Request
    exit; // we need a valid record and its ID
   end;

  aParams.Results([sfilename]);

  //aResp := JSONEncodeResult([SynCommons.BinToHex(aData)]);
  // idem: aResp := JSONEncode(['result',BinToHex(aRecord.fData)],TempMemoryStream);
  result := 200; // success
end;

Offline

#2 2012-12-07 09:53:14

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

Re: no longer works TSQLRestServerCallBackParams(aParams).SentData!

You need to change the method signature (i.e. definition): not function ...: integer but procedure.

Instead of result := ... you need to call Results() or Returns() or Error().

This is clearly stated by http://synopse.info/forum/viewtopic.php?id=958
and http://blog.synopse.info/post/2010/07/1 … phi-7-2010

Offline

#3 2012-12-07 10:29:25

corchi72
Member
Registered: 2010-12-10
Posts: 232

Re: no longer works TSQLRestServerCallBackParams(aParams).SentData!

ok I changed the function in procedure, but the sencod parameter is too long, so it does not even run the code on the server

Offline

#4 2012-12-07 11:02:22

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

Re: no longer works TSQLRestServerCallBackParams(aParams).SentData!

corchi72 wrote:

the sencod parameter is too long, so it does not even run the code on the server

What do you mean?

Offline

#5 2012-12-07 11:15:40

corchi72
Member
Registered: 2010-12-10
Posts: 232

Re: no longer works TSQLRestServerCallBackParams(aParams).SentData!

I found a solution in my first code I sent the whole xml in the server parameter "@aData" and I could not read it because the parameter sendData no longer exists.
But now I saw that the entire xml sent is inserted into the
  "sXML := Ctxt.Call.InBody". 'll explain with the code I correct:

Client side

 if FHttpDatabase.URI(format('%s/%s',FHttpDatabase.Model.Root,StringtoUTF8('XMLAsData')])+UrlEncode(['filename',StringtoUTF8(sfilename)]),'PUT',@aResponse,nil,@aData).Lo=200 then
begin
....
end;

Server side

procedure TFileServer.XMLAsData(var Ctxt: TSQLRestServerCallBackParams);
var //aData: TSQLRawBlob;
   sfilename:RawUTF8;
   XmlDoc: TNativeXml;
   aData: RawUTF8 ;
   Node: TXmlNode;
   sXML:RawUTF8;
begin
  //writeln('Start XMLAsData');
  if not UrlDecodeNeedParameters(Ctxt.Parameters,'FILENAME') then begin
     Ctxt.Error('Missing Parameter');;
   {$ifNdef SERVICE}
    writeln('invalid Request');
    {$ENDIF}
    exit;
  end;

  while Ctxt.Parameters<>nil do begin
    UrlDecodeValue(Ctxt.Parameters,'FILENAME=',sfilename,@Ctxt.Parameters);
  end;
   {$ifNdef SERVICE}
  writeln(format('filename: %s',[sfilename]));
   {$ENDIF}

  sXML := Ctxt.Call.InBody;
  if length(sXML)>0 then
  begin
    {$ifNdef SERVICE}
    if DebugHook<>0 then
     writeln(format('Exist data: %s',[sXML]));
    {$ENDIF}
     try
        XmlDoc := TNativeXml.Create(nil);
        XmlDoc.ReadFromString(UTF8ToString(sXML));

        XmlDoc.SaveToFile(sfilename);
    {$ifNdef SERVICE}
        writeln(format('Exist data: %s',[sfilename]));
    {$ENDIF}
     except
      on E: Exception do
      begin
     {$ifNdef SERVICE}
         writeln('Error launching the server' +#10+E.Message);
     {$ENDIF}
          Ctxt.Error('invalid Request');
         exit; // we need a valid record and its ID
      end;
     end;
     XmlDoc.Free;
  end
  else
   begin
    Ctxt.Error('invalid Request');
    exit; // we need a valid record and its ID
   end;

  Ctxt.Results([sfilename]);

end;

Offline

Board footer

Powered by FluxBB