API - Json-variable - multiple request

Hello,
I am using Node-red to perform API queries on modems.
The steps are as follows:
1 – Request 1: Initialize the connection with the modem - ***It's OK! ***
2 – Request 2: Retrieve the token then send a second request -- *** I'm already blocking HERE!!! ***
3 – Request 3: Wait for the modem's response to the previous request then send a 3rd request.
4 - Request 4: Wait for the modem's response to request no. 2 then send a 4th request

Blocking point :
I manage to recover my token in the debug, but I don't know how to write it as a variable in my second request

Maybe there is another way to do with tokens on node-red?
Information: This development is made to interrogate several hundreds of modems! so as input I would have a CSV file listing all the IP addresses of the different modems, but that's another story...

Thanks a lot for your help !

Mon Token : msg.payload.result.ubus_rpc_session

msg.payload = [{
    "jsonrpc": "2.0", "id": 1, "method": "call", "params":
        [
            "payload.result.ubus_rpc_session", "uci", "set",
            {
                "config": "sms_utils", "type": "rule", "match":
                {
                    "action": "reboot",
                    "enabled": "0"
                },
                "values":
                {
                    "action": "reboot",
                    "enabled": "1"
                }

Flow :

[{"id":"b56aaab0fb521d56","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"3b67977c.a53c08","type":"debug","z":"b56aaab0fb521d56","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":870,"y":120,"wires":[]},{"id":"4030c52c.c2c29c","type":"inject","z":"b56aaab0fb521d56","name":"API Teltonika","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":200,"wires":[["31727a2c.05a026"]]},{"id":"64d9243f.b7be7c","type":"http request","z":"b56aaab0fb521d56","name":"HTTP-REQUEST","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"headers":[],"x":630,"y":200,"wires":[["3b67977c.a53c08","ef71af4768ff47a3","3c3798b006a49db8","c6fbea8afe59bc01"]]},{"id":"31727a2c.05a026","type":"function","z":"b56aaab0fb521d56","name":"Requete","func":"msg.method = \"POST\"\nmsg.url = \"http://172.24.64.48/ubus\"\nmsg.headers = {}\nmsg.headers[\"content-type\"] = \"application/json\"\n\nmsg.payload = [{\n    \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\":\n        [\n            \"00000000000000000000000000000000\", \"session\", \"login\",\n            {\n                \"username\": \"root\", \"password\": \"XXXXX\"\n            }\n        ]\n}]\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":200,"wires":[["64d9243f.b7be7c"]]},{"id":"3c3798b006a49db8","type":"debug","z":"b56aaab0fb521d56","name":"Token","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.result.ubus_rpc_session","targetType":"jsonata","statusVal":"","statusType":"auto","x":870,"y":180,"wires":[]},{"id":"ef71af4768ff47a3","type":"debug","z":"b56aaab0fb521d56","name":"msgurl","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"url","targetType":"msg","statusVal":"","statusType":"auto","x":870,"y":220,"wires":[]},{"id":"c6fbea8afe59bc01","type":"function","z":"b56aaab0fb521d56","name":"Requete","func":"msg.method = \"POST\"\nmsg.url = msg.url\nmsg.headers = {}\nmsg.headers[\"content-type\"] = \"application/json\"\n\nmsg.payload = [{\n    \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\":\n        [\n            \"'payload[0].result[1].ubus_rpc_session'\", \"uci\", \"set\",\n            {\n                \"config\": \"sms_utils\", \"type\": \"rule\", \"match\":\n                {\n                    \"action\": \"reboot\",\n                    \"enabled\": \"0\"\n                },\n                \"values\":\n                {\n                    \"action\": \"reboot\",\n                    \"enabled\": \"1\"\n                }\n            }\n        ]\n}]\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":780,"y":360,"wires":[["0a011b68ae900ccb"]]},{"id":"0a011b68ae900ccb","type":"debug","z":"b56aaab0fb521d56","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1000,"y":360,"wires":[]}]

If I understand correctly
Try

msg.method = "POST"
msg.url = msg.url
msg.headers = {}
msg.headers["content-type"] = "application/json"

msg.payload = [{
    "jsonrpc": "2.0", "id": 1, "method": "call", "params":
        [
            msg.payload.result.ubus_rpc_session, "uci", "set",
            {
                "config": "sms_utils", "type": "rule", "match":
                {
                    "action": "reboot",
                    "enabled": "0"
                },
                "values":
                {
                    "action": "reboot",
                    "enabled": "1"
                }
            }
        ]
}]
return msg

Thank you for your answer
After trying it gives me an error

function: (error)
"TypeError: Cannot read properties of undefined (reading 'ubus_rpc_session')"

I had already tried with different quotes of type :

"'msg.payload[0].result[1].ubus_rpc_session'"

or

"msg.payload[0].result[1].ubus_rpc_session"

I also try to retrieve this token only in a variable but I don't know how to write it...?

Thanks a lot
:slightly_smiling_face:

Remove the quotes, as they make the property a string.

I took the value I used from your debug node, please show us the contents of msg debug copy the value using the debug side bar and paste here, so we can see exactly where your session value is.

A picture is worth a thousand words ...
I hope my request is understandable.

Sorry !

It 's OK !!!

msg.method = "POST"
msg.url = msg.url
msg.headers = {}
msg.headers["content-type"] = "application/json"

msg.payload = [{
    "jsonrpc": "2.0", "id": 1, "method": "call", "params":
        [
            msg.payload[0].result[1].ubus_rpc_session, "uci", "set",
            {
                "config": "sms_utils", "type": "rule", "match":
                {
                    "action": "reboot",
                    "enabled": "0"
                },
                "values":
                {
                    "action": "reboot",
                    "enabled": "1"
                }
            }
        ]
}]
return msg

No text is better as we can then copy and edit and paste with ease.

According to your debug

msg.payload[0].result[1].ubus_rpc_session

should work without quotes.

Thanks a lot !
Excuse me for my stupidity!

And your comment on the text is absolutely correct!

I'm going to abuse, but I immediately find myself blocked again.
How do I pass my token for my 3rd request?

Knowing that in the response, the API does not return this token to me since it is unchanged.

To test, I did:

[{"id":"b56aaab0fb521d56","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"3b67977c.a53c08","type":"debug","z":"b56aaab0fb521d56","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":40,"wires":[]},{"id":"4030c52c.c2c29c","type":"inject","z":"b56aaab0fb521d56","name":"API Teltonika","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":200,"wires":[["31727a2c.05a026"]]},{"id":"64d9243f.b7be7c","type":"http request","z":"b56aaab0fb521d56","name":"HTTP-REQUEST","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"headers":[],"x":630,"y":200,"wires":[["3b67977c.a53c08","ef71af4768ff47a3","3c3798b006a49db8","c6fbea8afe59bc01","1e1f7c03048a7d1a","df77e7a1fb2ed588"]]},{"id":"31727a2c.05a026","type":"function","z":"b56aaab0fb521d56","name":"Requete1-Init","func":"msg.method = \"POST\"\nmsg.url = \"http://172.24.64.48/ubus\"\nmsg.headers = {}\nmsg.headers[\"content-type\"] = \"application/json\"\n\nmsg.payload = [{\n    \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\":\n        [\n            \"00000000000000000000000000000000\", \"session\", \"login\",\n            {\n                \"username\": \"root\", \"password\": \"XXXXX\"\n            }\n        ]\n}]\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":200,"wires":[["64d9243f.b7be7c"]]},{"id":"3c3798b006a49db8","type":"debug","z":"b56aaab0fb521d56","name":"Token","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload[0].result[1].ubus_rpc_session","targetType":"jsonata","statusVal":"","statusType":"auto","x":790,"y":120,"wires":[]},{"id":"ef71af4768ff47a3","type":"debug","z":"b56aaab0fb521d56","name":"msgurl","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"url","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":80,"wires":[]},{"id":"c6fbea8afe59bc01","type":"function","z":"b56aaab0fb521d56","name":"Requete2","func":"msg.method = \"POST\"\nmsg.url = msg.url\nmsg.headers = {}\nmsg.headers[\"content-type\"] = \"application/json\"\nvar Token = msg.payload[0].result[1].ubus_rpc_session\n\nmsg.payload = [{\n    \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\":\n        [\n            msg.payload[0].result[1].ubus_rpc_session, \"uci\", \"set\",\n            {\n                \"config\": \"sms_utils\", \"type\": \"rule\", \"match\":\n                {\n                    \"action\": \"reboot\",\n                    \"enabled\": \"0\"\n                },\n                \"values\":\n                {\n                    \"action\": \"reboot\",\n                    \"enabled\": \"1\"\n                }\n            }\n        ]\n}]\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":840,"y":200,"wires":[["8013d218aee3317b"]]},{"id":"8013d218aee3317b","type":"http request","z":"b56aaab0fb521d56","name":"HTTP-REQUEST","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"headers":[],"x":1030,"y":200,"wires":[["18dae5571aabbf85","79f416be2966ddeb","1e1f7c03048a7d1a"]]},{"id":"1e1f7c03048a7d1a","type":"function","z":"b56aaab0fb521d56","name":"Requete3","func":"msg.method = \"POST\"\nmsg.url = msg.url\nmsg.headers = {}\nmsg.headers[\"content-type\"] = \"application/json\"\n\nmsg.payload = [{\n    \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\":\n        [\n            msg.payload[0].result[1].ubus_rpc_session, \"uci\", \"commit\",\n            {\n                \"config\": \"sms_utils\"\n            }\n        ]\n}]\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":420,"wires":[["3ca90fe036d60974"]]},{"id":"18dae5571aabbf85","type":"debug","z":"b56aaab0fb521d56","name":"Token","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1210,"y":280,"wires":[]},{"id":"79f416be2966ddeb","type":"debug","z":"b56aaab0fb521d56","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1210,"y":100,"wires":[]},{"id":"3ca90fe036d60974","type":"http request","z":"b56aaab0fb521d56","name":"HTTP-REQUEST","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"headers":[],"x":910,"y":420,"wires":[["df77e7a1fb2ed588"]]},{"id":"df77e7a1fb2ed588","type":"function","z":"b56aaab0fb521d56","name":"Requete4","func":"msg.method = \"POST\"\nmsg.url = msg.url\nmsg.headers = {}\nmsg.headers[\"content-type\"] = \"application/json\"\n\nmsg.payload = [{\n    \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\":\n        [\n            msg.payload[0].result[1].ubus_rpc_session, \"file\", \"exec\",\n            {\n                \"command\": \"luci-reload\"\n            }\n        ]\n}]\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":500,"wires":[["18731ae96bc4f13d"]]},{"id":"18731ae96bc4f13d","type":"http request","z":"b56aaab0fb521d56","name":"HTTP-REQUEST","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"headers":[],"x":910,"y":500,"wires":[["add6947129e7c35c"]]},{"id":"add6947129e7c35c","type":"debug","z":"b56aaab0fb521d56","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1130,"y":500,"wires":[]}]

But of course it doesn't work...

TypeError: Cannot read properties of undefined (reading 'ubus_rpc_session')

My request 3rd :

msg.method = "POST"
msg.url = msg.url
msg.headers = {}
msg.headers["content-type"] = "application/json"

msg.payload = [{
    "jsonrpc": "2.0", "id": 1, "method": "call", "params":
        [
            msg.payload[0].result[1].ubus_rpc_session, "uci", "commit",
            {
                "config": "sms_utils"
            }
        ]
}]
return msg

thank you again

you can use context storage to store the session
untested

[{"id":"31727a2c.05a026","type":"function","z":"b56aaab0fb521d56","name":"Requete1-Init","func":"msg.method = \"POST\"\nmsg.url = \"http://172.24.64.48/ubus\"\nmsg.headers = {}\nmsg.headers[\"content-type\"] = \"application/json\"\n\nmsg.payload = [{\n    \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\":\n        [\n            \"00000000000000000000000000000000\", \"session\", \"login\",\n            {\n                \"username\": \"root\", \"password\": \"Used@02!\"\n            }\n        ]\n}]\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":200,"wires":[["64d9243f.b7be7c"]]},{"id":"4030c52c.c2c29c","type":"inject","z":"b56aaab0fb521d56","name":"API Teltonika","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":200,"wires":[["31727a2c.05a026"]]},{"id":"64d9243f.b7be7c","type":"http request","z":"b56aaab0fb521d56","name":"HTTP-REQUEST","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":630,"y":200,"wires":[["3b67977c.a53c08","ef71af4768ff47a3","3c3798b006a49db8","c6fbea8afe59bc01","1e1f7c03048a7d1a","df77e7a1fb2ed588"]]},{"id":"3b67977c.a53c08","type":"debug","z":"b56aaab0fb521d56","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":40,"wires":[]},{"id":"ef71af4768ff47a3","type":"debug","z":"b56aaab0fb521d56","name":"msgurl","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"url","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":80,"wires":[]},{"id":"3c3798b006a49db8","type":"debug","z":"b56aaab0fb521d56","name":"Token","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload[0].result[1].ubus_rpc_session","targetType":"jsonata","statusVal":"","statusType":"auto","x":790,"y":120,"wires":[]},{"id":"c6fbea8afe59bc01","type":"function","z":"b56aaab0fb521d56","name":"Requete2","func":"msg.method = \"POST\"\nmsg.url = msg.url\nmsg.headers = {}\nmsg.headers[\"content-type\"] = \"application/json\"\nvar Token = msg.payload[0].result[1].ubus_rpc_session\nflow.set(\"session\", msg.payload[0].result[1].ubus_rpc_session)\nmsg.payload = [{\n    \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\":\n        [\n            msg.payload[0].result[1].ubus_rpc_session, \"uci\", \"set\",\n            {\n                \"config\": \"sms_utils\", \"type\": \"rule\", \"match\":\n                {\n                    \"action\": \"reboot\",\n                    \"enabled\": \"0\"\n                },\n                \"values\":\n                {\n                    \"action\": \"reboot\",\n                    \"enabled\": \"1\"\n                }\n            }\n        ]\n}]\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":840,"y":200,"wires":[["8013d218aee3317b"]]},{"id":"1e1f7c03048a7d1a","type":"function","z":"b56aaab0fb521d56","name":"Requete3","func":"msg.method = \"POST\"\nmsg.url = msg.url\nmsg.headers = {}\nmsg.headers[\"content-type\"] = \"application/json\"\nconst session = flow.get(\"session\") || \"error\";\nif(session === \"error\") node.warn(\"session error\");\n\nmsg.payload = [{\n    \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\":\n        [\n            session, \"uci\", \"commit\",\n            {\n                \"config\": \"sms_utils\"\n            }\n        ]\n}]\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":420,"wires":[["3ca90fe036d60974"]]},{"id":"df77e7a1fb2ed588","type":"function","z":"b56aaab0fb521d56","name":"Requete4","func":"msg.method = \"POST\"\nmsg.url = msg.url\nmsg.headers = {}\nmsg.headers[\"content-type\"] = \"application/json\"\n\nmsg.payload = [{\n    \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\":\n        [\n            msg.payload[0].result[1].ubus_rpc_session, \"file\", \"exec\",\n            {\n                \"command\": \"luci-reload\"\n            }\n        ]\n}]\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":500,"wires":[["18731ae96bc4f13d"]]},{"id":"8013d218aee3317b","type":"http request","z":"b56aaab0fb521d56","name":"HTTP-REQUEST","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":1030,"y":200,"wires":[["18dae5571aabbf85","79f416be2966ddeb","1e1f7c03048a7d1a"]]},{"id":"3ca90fe036d60974","type":"http request","z":"b56aaab0fb521d56","name":"HTTP-REQUEST","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":910,"y":420,"wires":[["df77e7a1fb2ed588"]]},{"id":"18731ae96bc4f13d","type":"http request","z":"b56aaab0fb521d56","name":"HTTP-REQUEST","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":910,"y":500,"wires":[["add6947129e7c35c"]]},{"id":"18dae5571aabbf85","type":"debug","z":"b56aaab0fb521d56","name":"Token","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1210,"y":280,"wires":[]},{"id":"79f416be2966ddeb","type":"debug","z":"b56aaab0fb521d56","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1210,"y":100,"wires":[]},{"id":"add6947129e7c35c","type":"debug","z":"b56aaab0fb521d56","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1130,"y":500,"wires":[]}]

https://nodered.org/docs/user-guide/context

Once stored in context it would be available to be called in any change or function or template node.

Thank you very much E1cid,
I'm going to lean a little more on the documentation because according to what you present to me, this is native with a good code! Thanks !

Unfortunately I have multiple simultaneous projects and I have to put this one aside, I hope to come back to it soon, I will keep you informed of my progress! thank you for your goodbye, have a nice day!

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