RTC Forums

Subscription => Support => Topic started by: brian71us on June 01, 2017, 10:12:16 PM



Title: Pass objects as parameters in XML-RPC methods
Post by: brian71us on June 01, 2017, 10:12:16 PM
Hello,

I am working on a server that uses XML-RPC for remote procedure calls as shown below:

    this.validateLogin = function(user, company, password) {
        console.log(JSON.toString(user) + ", " + JSON.toString(company) + ", " + password);
        xmlrpc.callMethod('login', [{
            User: user,
            Company: company,
            Password: password
        }]).then(function(response){
            console.log(response);
            if (response === 'OK') {
                return true;
            } else if (response === 'Fail') {
                return false;
            };
        }, function(err){
            console.log("Error validating login: " + JSON.stringify(err));
        });
    };

Password is a string. User and Company are JavaScript objects.

What I receive on the server is show below.

'(Oid={8D03643A-CAEA-4A21-8563-3E0890E30634}; UserName=Zachary Cobb; CertificationNumber=OSHA-CERT007; Companies=(Oid={D014A27E-0003-4A5D-8BD7-E37A772DEEDC}; Name=Remarkable Scales & Service; $$hashKey=object:18); $$hashKey=object:6)'

The TJSONObject.ParseJSONValue() method returns nil when I pass this string to it.

The only way that I can get this all to work properly is to stringify the objects.

    this.validateLogin = function(user, company, password) {
        console.log(JSON.toString(user) + ", " + JSON.toString(company) + ", " + password);
        xmlrpc.callMethod('login', [{
            User: JSON.stringify(user),
            Company: JSON.stringify(company),
            Password: password
        }]).then(function(response){
            console.log(response);
            if (response === 'OK') {
                return true;
            } else if (response === 'Fail') {
                return false;
            };
        }, function(err){
            console.log("Error validating login: " + JSON.stringify(err));
        });
    };


Is there a way to handle this without calling stringify for every object?


Here is the current implementation of the function.

procedure TUserModule.loginExecute(Sender: TRtcConnection;
  Param: TRtcFunctionInfo; Result: TRtcValue);
var
  Company: TJSONObject;
  User: TJSONObject;
  Password: String;
  Parameter: String;
begin
  // Get the parameters
  Parameter := Param.asString['User'];

  User := TJSONObject.ParseJSONValue(Parameter) as TJSONObject;

  Company := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(Param.asString['Company']),0) as TJSONObject;
  Password := Param.asString['Password'];

  if ValidatePassword(User.Values['Oid'].Value, Password) then
    Result.asString := 'OK'
  else
    Result.asString := 'Fail';
end;


Title: Re: Pass objects as parameters in XML-RPC methods
Post by: D.Tkalcec (RTC) on June 02, 2017, 11:10:32 AM
It seems to me that you are talking about a problem you have with JavaScript. Since JavaScript is executed inside the Browser and NOT inside the Server, you should check JavaScript documentation and/or any documentation available for JavaScript libraries you are using to see if you can get the results you wanted.

As for the RTC Server-side code, whatever the Client sents to the Server (in your case, the Web Browser executing your JavaScript code), should be available to you in the OnExecute event, exactly the way it was sent.

PS. Since you are already using JSON to serialize parameters for your remote function call, you might want to check the new JSON formats available in the TRtcServerModule component (in RTC SDK v8) and either use JSON-RPC or JSON over REST from JavaScript to make remote function calls, instead of mixing XML-RPC and JSON.

Best Regards,
Danijel Tkalcec


Title: Re: Pass objects as parameters in XML-RPC methods
Post by: brian71us on June 02, 2017, 03:35:50 PM
Ooooohhhh... shiny!  ;D

This is a work in progress and we have a big presentation next week. I was planning to update to the latest after that.