RTC Forums
November 23, 2024, 09:37:51 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Pass objects as parameters in XML-RPC methods  (Read 4887 times)
brian71us
RTC Expired
*
Posts: 15


« 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;
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 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
Logged
brian71us
RTC Expired
*
Posts: 15


« Reply #2 on: June 02, 2017, 03:35:50 PM »

Ooooohhhh... shiny!  Grin

This is a work in progress and we have a big presentation next week. I was planning to update to the latest after that.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.024 seconds with 17 queries.