[Pas2js] Pas2js issue in posting a modified record - TRESTDataProxy

Simone Giacomelli simone.giacomelli at gmail.com
Sun Feb 23 14:22:47 CET 2020


Dear coders,

I think I found a fix for a problem (?) in the function below.

The problem is that with the code in pas2js 1.4.18 you will get a 401
error Unauthorized.
I just checked also in the svn trunk and it is the same as explained
above: https://svn.freepascal.org/svn/projects/pas2js/trunk/packages/fcl-db/restconnection.pas

This solution was inspired by the code from the function
TRESTDataProxy.DoGetData(...)

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]

function TRESTDataProxy.ProcessUpdateBatch(aBatch: TRecordUpdateBatch): Boolean;

Var
  R : TRESTUpdateRequest;
  i : Integer;
  Method,URl : String;

begin
  Result:=False;
  For I:=0 to aBatch.List.Count-1 do
    begin
    R:=aBatch.List[i] as TRESTUpdateRequest;
    R.FBatch:=aBatch;
    R.FXHR:=TJSXMLHttpRequest.New;
    R.FXHR.AddEventListener('load', at R.onLoad);
    URL:=FConnection.GetRecordUpdateURL(R);
    Case R.Status of
      usInserted :
        Method:='POST';
      usModified:
        Method:='PUT';
      usDeleted:
        Method:='DELETE';
    end;
    R.FXHR.open(Method,URL);
    Connection.SetupRequest(R.FXHR); // <--------- I ADDED THIS LINE.
Without it you get a 401
    R.FXHR.setRequestHeader('content-type','application/json');
    if R.Status in [usInserted,usModified] then
      R.FXHR.Send(TJSJSON.Stringify(R.Data))
    else
      R.FXHR.Send;
    end;
  Result:=True;
end;

[/code]

-- 
Simone


More information about the Pas2js mailing list