Search code examples
node-red

Node-Red API call with flow store from previous API call


I have the following Node-Red Flow to authenticate with an API, retrieve token from it, save it to flow variable and then call a new API endpoint with the token:

[
    {
        "id": "4d1f98b0.642758",
        "type": "http request",
        "z": "c155aa476d7d3fe2",
        "name": "/login",
        "method": "POST",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "https://eu5.fusionsolar.huawei.com/thirdData/login",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 590,
        "y": 180,
        "wires": [
            [
                "9e0cdaf0c53abc05"
            ]
        ]
    },
    {
        "id": "62206b38.707924",
        "type": "function",
        "z": "c155aa476d7d3fe2",
        "name": "prepare /login",
        "func": "msg.headers = {}\nmsg.url = \"https://eu5.fusionsolar.huawei.com/thirdData/login\";\nmsg.headers[\"content-type\"] = \"application/json\"\n\n\nmsg.payload = {\n    \"userName\": \"ASDF\",\n    \"systemCode\": \"FDSA\"\n}\n\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 400,
        "y": 180,
        "wires": [
            [
                "4d1f98b0.642758"
            ]
        ]
    },
    {
        "id": "fda2c97918f48fc3",
        "type": "inject",
        "z": "c155aa476d7d3fe2",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 180,
        "y": 180,
        "wires": [
            [
                "62206b38.707924"
            ]
        ]
    },
    {
        "id": "0ce4ec69bf9d8b8a",
        "type": "debug",
        "z": "c155aa476d7d3fe2",
        "name": "debug msg",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1510,
        "y": 320,
        "wires": []
    },
    {
        "id": "9e0cdaf0c53abc05",
        "type": "function",
        "z": "c155aa476d7d3fe2",
        "name": "flow save token",
        "func": "flow.set('token', msg.headers[\"xsrf-token\"]);\n\n// var v1 = flow.get(\"token\");\n// node.log(v1);",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 760,
        "y": 260,
        "wires": [
            [
                "7a3e4af0ea3ffca9"
            ]
        ]
    },
    {
        "id": "7a3e4af0ea3ffca9",
        "type": "function",
        "z": "c155aa476d7d3fe2",
        "name": "prepare /getDevRealKpi",
        "func": "msg.headers = {}\nmsg.url = \"https://eu5.fusionsolar.huawei.com/thirdData/getDevRealKpi\";\nmsg.headers[\"content-type\"] = \"application/json\"\nmsg.headers[\"XSRF-TOKEN\"] = flow.get(\"token\");\n\nmsg.payload = {\n    \"devIds\": \"1337\",\n    \"devTypeId\": \"1\"\n}\n\nnode.log(msg.payload);\n\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1050,
        "y": 320,
        "wires": [
            [
                "45bbc3580423f628"
            ]
        ]
    },
    {
        "id": "45bbc3580423f628",
        "type": "http request",
        "z": "c155aa476d7d3fe2",
        "name": "/getDevRealKpi",
        "method": "POST",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "https://eu5.fusionsolar.huawei.com/thirdData/getDevRealKpi",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 1300,
        "y": 320,
        "wires": [
            [
                "0ce4ec69bf9d8b8a"
            ]
        ]
    },
    {
        "id": "f2fa10e69d612fbb",
        "type": "inject",
        "z": "c155aa476d7d3fe2",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "token",
        "payloadType": "flow",
        "x": 800,
        "y": 380,
        "wires": [
            [
                "7a3e4af0ea3ffca9"
            ]
        ]
    }
]

flow

flow save token:

flow.set('token', msg.headers[\"xsrf-token\"]);
// var v1 = flow.get(\"token\");
// node.log(v1);

My problem is that the second function prepare /getDevRealKpi does not get triggered and therefore not the second API call. I can only use it when I use the manual injection flow.token.


Solution

  • The problem is that your "flow save token" function doesn't return anything.

    If a function node doesn't return anything then it won't send a message to trigger the next node in the flow.

    Add return msg to end.

    flow.set('token', msg.headers[\"xsrf-token\"]);
    // var v1 = flow.get(\"token\");
    // node.log(v1);
    return msg;