javascripthttprequestnode-red

node-red HTTP authentication through environmental variables


I want to perform a GET request to an API but I want to make sure the endpoint, username and password are managed as environmental variabled because my node application will have multiple environments (dev/test/production).

I have added the environmental variables on the flow. enter image description here

Now I'm trying to set the correct attributes on the msg object to replace the values with the environmental variables. enter image description here

It's already working for the url but I'm not able to replace the username and password. I cannot find the correct attribute names anywhere in the docs. I can imagine that a lot of people would have simular requirements when working with multiple-tier systems.

So what is in fact the correct way to achieve this?

msg.url = 
 env.get("MendixURL")
 + "/rest/shopfloorintegration/v1/message?PlantCode="
 + env.get("PlantCode");

msg.authentication = "basic";
msg.credentials = {
    username : env.get("MendixUsername"),
    password : env.get("MendixPassword")
};


return msg;

enter image description here


Solution

  • Don't try and set them in the function node.

    In the HTTP-Request node put ${MendixUsername} in the Username field and ${MendixPassword} in the Password filed.

    e.g.

    enter image description here

    or you can encode username and password into the URL you are building in the function node.

    e.g.

    http://username:password@www.example.com/