# How to use variables entered in editor inside httpadmin function

**URL:** <https://discourse.nodered.org/t/how-to-use-variables-entered-in-editor-inside-httpadmin-function/73194>\
**Category:** Developing Nodes\
**Created:** [5 January 2023 03:09 UTC](https://discourse.nodered.org/t/how-to-use-variables-entered-in-editor-inside-httpadmin-function/73194 "2023-01-05T03:09:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![syedhannan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/syedhannan/32/72932_2.png) [@syedhannan](https://discourse.nodered.org/u/syedhannan)\
**Post date:** [5 January 2023 03:09 UTC](https://discourse.nodered.org/t/how-to-use-variables-entered-in-editor-inside-httpadmin-function/73194/1 "2023-01-05T03:09:40Z")

</div>

I am trying to display a list of files from FTP server after successful authentication using **basic-ftp** package.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6f89e8e0868cce67e6a4f66c27d7a48e974ca75f.png)

I want to use the variables entered in the editor inside the httpadmin function which makes a ftp call instead of the hard coded values shown below.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1eeb3a3f2ceb2ffa6ed7f0f1563a8a3b412d556c.png)

Is there a way I can pass the variables entered in editor inside the httpadmin call function

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [5 January 2023 07:08 UTC](https://discourse.nodered.org/t/how-to-use-variables-entered-in-editor-inside-httpadmin-function/73194/2 "2023-01-05T07:08:41Z")

</div>

There are various ftp nodes already available.

If you want to use an npm module, you could also do this directly in a function node.

Enable this feature in `settings.js`:

```auto
   /** Allow the Function node to load additional npm modules directly */
    functionExternalModules: true,

```

Restart node-red.

Then type the module package name in the `Setup` tab and node-red will install it in the background:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/33e17caa09143a5bc1ea317ddae28e0e4db6c9c8.png)

and then in the "On Message" tab:

```auto
const client = new ftp.Client()
client.ftp.verbose = true
try {
    await client.access({
        host: msg.payload.ip,
        user: msg.payload.username,
        password: msg.payload.password,
        secure: false
    })
    
    node.send({payload:await client.list()}) // outputs the files in the directory

}
catch (err) {
    node.warn(err)
}
client.close()

```

Then you can pass properties directly without the need to create a full node.

---

<div class="post-metadata">

**Author:** ![syedhannan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/syedhannan/32/72932_2.png) [@syedhannan](https://discourse.nodered.org/u/syedhannan)\
**Post date:** [5 January 2023 07:44 UTC](https://discourse.nodered.org/t/how-to-use-variables-entered-in-editor-inside-httpadmin-function/73194/3 "2023-01-05T07:44:17Z")

</div>

Let me explain the complete flow:  
I am trying to run ansible command with this node which I am creating.

So after entering correct details in editor the user will be connected to ftp server which then displays a list of files present in the server on the editor itself. So once the user selects the file and deploys it starts executing the ansible command.

So I wanted to know the following:

1. Can we combine an already available ftp node on the editor of my custom node.
2. Is it possible to pass the values entered in text fields to the **httpadmin** function where I can use the basic-ftp module and display the list of files.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [5 January 2023 11:55 UTC](https://discourse.nodered.org/t/how-to-use-variables-entered-in-editor-inside-httpadmin-function/73194/4 "2023-01-05T11:55:17Z")

</div>

> [@syedhannan](#):
>
> **basic-ftp**

I hope that you are going to make FTPS and SFTP and maybe SCP as secure options? In my professional realm, we don't allow raw FTP even internally since passing in the user and password happens unencrypted

> [@syedhannan](#):
>
> Can we combine an already available ftp node on the editor of my custom node.

No, each one has its own nodes. So you can include them in your flows but not in your own nodes.

> [@syedhannan](#):
>
> Is it possible to pass the values entered in text fields to the **httpadmin** function where I can use the basic-ftp module and display the list of files.

I'm a bit confused about the "httpadmin" part? What do you mean by that? I assume that you mean the Node-RED Editor?

Certainly you can use fields entered in your custom node's Editor panel, that is the point. Have a look in the Node-RED docs concerning creating custom nodes. It explains how you do it.

---

<div class="post-metadata">

**Author:** ![syedhannan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/syedhannan/32/72932_2.png) [@syedhannan](https://discourse.nodered.org/u/syedhannan)\
**Post date:** [9 January 2023 04:52 UTC](https://discourse.nodered.org/t/how-to-use-variables-entered-in-editor-inside-httpadmin-function/73194/5 "2023-01-09T04:52:20Z")

</div>

> [@TotallyInformation](#):
>
> I'm a bit confused about the "httpadmin" part? What do you mean by that? I assume that you mean the Node-RED Editor?

I was referring to this httpAdmin function. So instead of the hard coded values in host, user etc. I want to pass the values obtained from my custom node's editor panel which I am not sure how it is done.

```auto
RED.httpAdmin.get(
    "/ftplist",
    RED.auth.needsPermission("ftplist.read"),

    async function (req, res) {
      t;

      ftpList = [];
      ftpListNames = [];
      hostAddress = "1";

      const client = new ftp.Client();
      client.ftp.verbose = true;
      try {
        await client.access({
          host: "192.168.109.140", //hard coded
          user: "testuser",
          password: "1234",
        });

        ftpList = await client.list("/files");

        for (let i = 0; i < ftpList.length; i++) {
          ftpListNames.push(ftpList[i].name);
        }
      } catch (err) {
        console.log(err);
      }
      client.close();
      res.json(ftpListNames);
    }
  );

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 January 2023 09:27 UTC](https://discourse.nodered.org/t/how-to-use-variables-entered-in-editor-inside-httpadmin-function/73194/6 "2023-01-09T09:27:33Z")

</div>

> [@syedhannan](#):
>
> `RED.httpAdmin`

That is a reference to the ExpressJS web server that serves up the Editor and other Node-RED admin features. That function is adding "middleware" to Express to define a new endpoint `/ftplist`. I expect you know that but just setting the scene for anyone else who might not.

It is basically setting up a new API that requires Editor permissions to access.

The settings from the Editor for your node are passed to your node's runtime js in the part of your js that is run when it calls `RED.nodes.registerType` That call takes a callback function that has a parameter typically referred to as `config`. In that callback, you typically transfer the settings from the front-end `config` object to the node (which represents the instance of your custom node definition). That is done by calling `RED.nodes.createNode(this, config)` and following that, `this` represents the instance of your node and you transfer the config settings to it with a series of statements like `this.name = config.name ?? `.

In my own custom nodes, I use a particular layout which I think is easier to parse. Check it out in [node-red-contrib-uibuilder/customNode.js at v6.1.0 · TotallyInformation/node-red-contrib-uibuilder (github.com)](https://github.com/TotallyInformation/node-red-contrib-uibuilder/blob/v6.1.0/nodes/uib-element/customNode.js) as an example.

So getting back to the question, to be able to pick up the settings, you define the `get` callback in a position that has access to the node instance definition. I don't have a simple example I'm afraid because uibuilder - which makes use of several admin API's as well as several user-facing API's - is quite complex and has split the "web" type functions into a separate class. However, you will find an example here:

> <https://github.com/TotallyInformation/node-red-contrib-uibuilder/blob/v6.1.0/nodes/uibuilder.js#L417-L420>

which is, as I describe above, in the callback function used when the node instance is actually instanciated. As you can see, what is sent back to the requestor (which will be in your node's html file) is actually provided by a call to a `web` class method. That gets the node instance object (`this`) passed to it in an argument along with the ExpressJS `req` (request) object.

PS: In most example code, you will see people immediately create a variable called `node` and pass `this` to it. I don't do that in my layout because it isn't needed. However, if I do pass the node instance to another function/method, I always call it `node` in the arguments.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [9 January 2023 09:39 UTC](https://discourse.nodered.org/t/how-to-use-variables-entered-in-editor-inside-httpadmin-function/73194/7 "2023-01-09T09:39:45Z")

</div>

@syedhannan if you want to use the values entered in the edit dialog, you can pass them over in the request you send to the backend.

The Inject node includes an example of how to do this with the 'inject now' button that was added in a recent release.

Rather than use a `GET` request, it uses a `POST` request, and passes the properties as the body of the request.

You can see the runtime side here: [node-red/20-inject.js at master · node-red/node-red · GitHub](https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/nodes/core/common/20-inject.js#L154)

And the editor side here: [node-red/20-inject.html at master · node-red/node-red · GitHub](https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/nodes/core/common/20-inject.html#L202)

The key parts are using the `data` property in the `$.ajax(...)` call to set the body of the request and setting `contentType: "application/json; charset=utf-8"` so the runtime knows how to parse it. Then in the runtime, accessing the values as `req.body`.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 January 2023 09:40 UTC](https://discourse.nodered.org/t/how-to-use-variables-entered-in-editor-inside-httpadmin-function/73194/8 "2023-01-23T09:40:42Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
