Passing Arguments to an external script

Hi all,

I want to create a new topic after updating this topic since it is an old one and may be not related to my problem.

I have an external script that creates an XMPP user for CPEs:

"use strict"
const http = require("http");

let cache = null;
let cacheExpire = 0;

function addUser(args, callback) {
  if (Date.now() < cacheExpire) return callback(null, cache);
  
  var test = JSON.stringify({
      "user": args[0];
      "host": "localhost";
      "password": args[1];
  });
  
  const options = {
        hostname: '127.0.0.1',
        path: '/api/register',
        port: 5281,
        method: 'POST',
        headers: {
             'Content-Type': 'application/x-www-form-urlencoded',
             'Content-Length': test.length
        },
        body: test  };
  const req = http.request(options, (res) => {
  var chunks = [];
  
  if (res.statusCode !== 200) {
      return callback(null, null);
  }
  
  res.on("data", function (chunk) {
    chunks.push(chunk);
  });
  
  res.on("end", function (chunk) {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
    var result = JSON.parse(body.toString());
    callback(null, result);
  });
});

  req.on("error", function (error) {
    callback(error);
  });

req.write(test);
req.end();
}

exports.addUser = addUser;

And this is called in provision:

const res = ext("ext-config", "addUser", "username","password");

But sometimes I have wrong arguments in my script such as the password is “(s,c)=>{e.delete(t[0])&&process.send([t[0],n(s),c])}” as in the screenshot.

So even API call is successful, I got a timeout error in provision. Do I have any errors in my script?

Try using “arguments” array instead of “args”

"user": arguments[0][0];
"password": arguments[0][1];

Loggin json.stringify of “arguments” may help.

Thank you,

It looks like I messed up with the JSON format by using semicolons instead of commas. So changed with this and no problem:

var test = JSON.stringify({
          "user": args[0],
        "host": "localhost",
        "password": args[1]
});
1 Like

Just randomly specifying a keyword will not make things work. If you review the code snippet, the parameter is clearly named args, not arguments.

My suggestion is to JSON encode all the data passed into the external script, then JSON.parse it in the script to extract out what you need. The big advantage of this is you are not tied to ordinal positioning for parameter values. This will reduce the cognative load when you have to look at the ext script in a years time.

Heres how I do it:

// Some provision script
const serial = declare('DeviceID.SerialNumber', {value: 1}).value[0];
const productClass = declare('DeviceID.ProductClass', {value: 1}).value[0];
const oui = declare('DeviceID.OUI', {value: 1}).value[0];
const deviceId = declare('DeviceID.ID', {value: 1}).value[0];
const params = {serial, productClass, oui, deviceId};

//Get the PPPoE creds
let config = ext('extensions', 'resetPppoe', JSON.stringify(params));
...
// extensions.js
function resetPppoe(args, callback) {
    const params = JSON.parse(args[0]);
    const uri = `${API_URL}ResetPPPoECreds?serial=${params.serial}&productClass=${params.productClass}&oui=${params.oui}&deviceId=${encodeURIComponent(params.deviceId)}`;
...
2 Likes

Dear @akcoder
I use your solution, but my simple http server didn’t receive any request.
Could you check my post at Integration with external http server ?