# Passing Arguments to an external script

**URL:** <https://forum.genieacs.com/t/passing-arguments-to-an-external-script/3563>\
**Category:** Uncategorized\
**Created:** [February 20, 2023, 10:56am UTC](https://forum.genieacs.com/t/passing-arguments-to-an-external-script/3563 "2023-02-20T10:56:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![usta11](https://avatars.discourse-cdn.com/v4/letter/u/ecb155/32.png) [@usta11](https://forum.genieacs.com/u/usta11)\
**Post date:** [February 20, 2023, 10:56am UTC](https://forum.genieacs.com/t/passing-arguments-to-an-external-script/3563/1 "2023-02-20T10:56:41Z")

</div>

Hi all,

I want to create a new topic after updating [this topic](https://forum.genieacs.com/t/external-script-calling-external-api-timed-out/609) 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:

```auto
"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:

```auto
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.

 ![Screenshot from 2023-02-20 13-26-06](https://canada1.discourse-cdn.com/flex035/uploads/genieacs/original/2X/3/35728a3745677334e8521b1ac2b1fcb95e3a7186.png)

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

---

<div class="post-metadata">

**Author:** ![Monkito](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/monkito/32/1224_2.png) [@Monkito](https://forum.genieacs.com/u/Monkito)\
**Post date:** [February 21, 2023, 4:14am UTC](https://forum.genieacs.com/t/passing-arguments-to-an-external-script/3563/2 "2023-02-21T04:14:49Z")

</div>

Try using “arguments” array instead of “args”

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

```

Loggin json.stringify of “arguments” may help.

---

<div class="post-metadata">

**Author:** ![usta11](https://avatars.discourse-cdn.com/v4/letter/u/ecb155/32.png) [@usta11](https://forum.genieacs.com/u/usta11)\
**Post date:** [February 21, 2023, 10:23am UTC](https://forum.genieacs.com/t/passing-arguments-to-an-external-script/3563/3 "2023-02-21T10:23:01Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![akcoder](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/akcoder/32/11_2.png) [@akcoder](https://forum.genieacs.com/u/akcoder)\
**Post date:** [February 21, 2023, 6:08pm UTC](https://forum.genieacs.com/t/passing-arguments-to-an-external-script/3563/4 "2023-02-21T18:08:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![akcoder](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/akcoder/32/11_2.png) [@akcoder](https://forum.genieacs.com/u/akcoder)\
**Post date:** [February 21, 2023, 6:12pm UTC](https://forum.genieacs.com/t/passing-arguments-to-an-external-script/3563/5 "2023-02-21T18:12:49Z")

</div>

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:

```auto
// 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));
...

```

```auto
// 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)}`;
...

```

---

<div class="post-metadata">

**Author:** ![binooetomo](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/binooetomo/32/1297_2.png) [@binooetomo](https://forum.genieacs.com/u/binooetomo)\
**Post date:** [March 31, 2023, 6:10am UTC](https://forum.genieacs.com/t/passing-arguments-to-an-external-script/3563/6 "2023-03-31T06:10:03Z")

</div>

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](https://forum.genieacs.com/t/integration-with-external-http-server/3750) ?
