Recent

Author Topic: Pas2js issue in posting a modified record - TRESTDataProxy  (Read 1836 times)

Jako

  • Newbie
  • Posts: 3
Pas2js issue in posting a modified record - TRESTDataProxy
« on: February 23, 2020, 02:08:05 pm »
Dear coders,

I think I found a fix for a problem in the function below.
Where should I report this issue?

The problem is that with the code in pas2js 1.4.18 you will get a 401 error Unauthorized.

This solution was inspired by the code from the function TRESTDataProxy.DoGetData(...)
The file that contains these functions is pas2js\packages\fcl-db\restconnection.pas

If you enable the log (through TSQLDBRestDispatcher.OnLog ) you will see the following message:
(User: ?) Resource: TEST1; Operation: PUT; Status: 401; Text: Unauthorized

Best regards
Simone Giacomelli

Code: Pascal  [Select][+][-]
  1.  
  2. function TRESTDataProxy.ProcessUpdateBatch(aBatch: TRecordUpdateBatch): Boolean;
  3.  
  4. Var
  5.   R : TRESTUpdateRequest;
  6.   i : Integer;
  7.   Method,URl : String;
  8.  
  9. begin
  10.   Result:=False;
  11.   For I:=0 to aBatch.List.Count-1 do
  12.     begin
  13.     R:=aBatch.List[i] as TRESTUpdateRequest;
  14.     R.FBatch:=aBatch;
  15.     R.FXHR:=TJSXMLHttpRequest.New;
  16.     R.FXHR.AddEventListener('load',@R.onLoad);
  17.     URL:=FConnection.GetRecordUpdateURL(R);
  18.     Case R.Status of
  19.       usInserted :
  20.         Method:='POST';
  21.       usModified:
  22.         Method:='PUT';
  23.       usDeleted:
  24.         Method:='DELETE';
  25.     end;
  26.     R.FXHR.open(Method,URL);
  27.     Connection.SetupRequest(R.FXHR); // <--------- I ADDED THIS LINE. Without it you get a 401  
  28.     R.FXHR.setRequestHeader('content-type','application/json');  
  29.     if R.Status in [usInserted,usModified] then
  30.       R.FXHR.Send(TJSJSON.Stringify(R.Data))
  31.     else
  32.       R.FXHR.Send;
  33.     end;
  34.   Result:=True;
  35. end;
  36.      
  37.  

MvC

  • New Member
  • *
  • Posts: 25
    • Free Pascal Core team member
Re: Pas2js issue in posting a modified record - TRESTDataProxy
« Reply #1 on: February 23, 2020, 08:23:19 pm »
Thank you for posting this. You are correct, this is an oversight.
I have modified the code in SVN.


 

TinyPortal © 2005-2018