Retrieve HTTP POST cUrl data on node-red

Hello,
I am new to node-red. I would like to know how I can recover HTTP Post Curl data on node red. I leave you the example:
curl -H "Content-Type:application/x-www-form-urlencoded" -c ./cookie.txt -X POST --data '{"id":"1","execute":1,"core" :"user","function":"login","values":[{"username":"admin","password":"sI/7ewBCeWunDs6JXXtSHg=="}]}' http://192.168.45.106/ cgi

Hi and Welcome to the forums!

Not sure I follow completely.
are you using curl to send data to Node RED?

or are you wishing to make a request from within Node RED to a resource and capture the response?

the goal is to retrieve data from a router using this Curl line.
yes I would like to make a request from Node RED to a resource and capture the response.

This should get you going (import it)

[{"id":"be09724a5e928145","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"158ae5b9d074d506","type":"http request","z":"be09724a5e928145","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"http://192.168.45.106/cgi","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"credentials":{},"x":570,"y":340,"wires":[["95d5415bcb2a2157"]]},{"id":"95d5415bcb2a2157","type":"debug","z":"be09724a5e928145","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":420,"wires":[]},{"id":"0a039e922c5778a9","type":"function","z":"be09724a5e928145","name":"Format Request","func":"const Message = {\n    \"payload\": {\n        \"id\": \"1\",\n        \"execute\": 1,\n        \"core\": \"user\",\n        \"function\": \"login\",\n        \"values\": [{\n            \"username\": \"admin\",\n            \"password\": \"sI/7ewBCeWunDs6JXXtSHg==\"\n        }]\n    },\n    \"headers\": {\n        \"Content-Type\": \"application/x-www-form-urlencoded\"\n    }\n}\n\nreturn Message","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":280,"wires":[["158ae5b9d074d506"]]},{"id":"6607ecc1878dccb0","type":"inject","z":"be09724a5e928145","name":"Trigger","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":220,"wires":[["0a039e922c5778a9"]]}]

EDIT:
Depending on the expected response content type.
you can change the return type on the request node.

you might need to tweak the payload especially values - i.e single quote the object.

const Message = {
    "payload": {
        "id": "1",
        "execute": 1,
        "core": "user",
        "function": "login",
        "values": '[{"username": "admin", "password": "sI/7ewBCeWunDs6JXXtSHg==" }]'
    },
    "headers": {
        "Content-Type": "application/x-www-form-urlencoded"
    }
}

return Message

I did the same thing as you, but I got an error message.

node-red1

I have a question what is it for, do I have to add it somewhere

I have a question what is it for, do I have to add it somewhere

Its a flow that should achieve what you need

Copy this text (I have corrected the payload)

[{"id":"be09724a5e928145","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"158ae5b9d074d506","type":"http request","z":"be09724a5e928145","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"http://localhost:1880/cgi","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":570,"y":340,"wires":[["95d5415bcb2a2157"]]},{"id":"95d5415bcb2a2157","type":"debug","z":"be09724a5e928145","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":420,"wires":[]},{"id":"0a039e922c5778a9","type":"function","z":"be09724a5e928145","name":"Format Request","func":"const Message = {\n    \"payload\": {\n        \"id\": \"1\",\n        \"execute\": 1,\n        \"core\": \"user\",\n        \"function\": \"login\",\n        \"values\": '[{\"username\": \"admin\", \"password\": \"sI/7ewBCeWunDs6JXXtSHg==\" }]'\n    },\n    \"headers\": {\n        \"Content-Type\": \"application/x-www-form-urlencoded\"\n    }\n}\n\nreturn Message","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":280,"wires":[["158ae5b9d074d506"]]},{"id":"6607ecc1878dccb0","type":"inject","z":"be09724a5e928145","name":"Trigger","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":220,"wires":[["0a039e922c5778a9"]]}]

and then import it.

of course, editing anything necessary (like the URL in the request node)

When I change the ip address I get this error message :
node-red4

To explain things to you the objective is to read the cellular data usage as below.
Access Cookie:

curl -H "Content-Type:application/x-www-form-urlencoded" -c ./cookie.txt -X POST --data '{"id":"1","execute":1,"core":"user","function":"login","values":[{"username":"admin","password":"sI/7ewBCeWunDs6JXXtSHg=="}]}' http://192.168.45.106/cgi

Inquiry data usage:

curl -H "Content-Type:application/x-www-form-urlencoded" -b ./cookie.txt -X POST --data '{"id":7,"execute":1,"core":"yruo_status","function":"get","values":[{"base":"yruo_celluar"}]}' http://192.168.45.106/cgi

Then you can get like below:

Ok - the response might be of an old HTTP version, and may not be supported by NodeJS.

give me a minute.
we can try executing curl from Node RED.

Okay take your time.

try this - its using the exec node to call curl

[{"id":"be09724a5e928145","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"8b03ba66b044c3d7","type":"exec","z":"be09724a5e928145","command":"/usr/bin/curl ","addpay":"payload","append":"","useSpawn":"false","timer":"2","winHide":false,"oldrc":false,"name":"Curl","x":570,"y":320,"wires":[["570c1643b0bde49f"],[],[]]},{"id":"f454c6183b821c86","type":"inject","z":"be09724a5e928145","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":240,"wires":[["8fa43d6b7c972b98"]]},{"id":"8fa43d6b7c972b98","type":"function","z":"be09724a5e928145","name":"Pass Args","func":"msg.payload = '-H \"Content-Type:application/x-www-form-urlencoded\" -c ./cookie.txt -X POST --data \\'{\"id\":\"1\",\"execute\":1,\"core\" :\"user\",\"function\":\"login\",\"values\":[{\"username\":\"admin\",\"password\":\"sI/7ewBCeWunDs6JXXtSHg==\"}]}\\' http://192.168.45.106/cgi'\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":320,"wires":[["8b03ba66b044c3d7"]]},{"id":"570c1643b0bde49f","type":"debug","z":"be09724a5e928145","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":180,"wires":[]}]

I think the error you was getting, was due to some response format not being accepted by the NodeJS runtime - i.e an old HTTP Version or something like that.

Great, it's working fine. thank you for your help.

1 Like

I have a small question, how can I do to keep just the value that is in yellow

node-red4

Can you paste the result as a JSON string here?
its really difficult to engineer from a screenshot

remove/change anything that is of sensitive data of course

I want to get the value in bold

{"id":7,"model":"UR32","pn":"L04EU2211110FR0000000000","oem":"0000","rtver":"32.3.0.5","status":0,"result":[{"get":[{"type":"yruo_cell_status","index":0,"value":{"modem":{"modem_status":"No SIM Card","model":"EC25","version":"EC25EUXGAR08A03M1G","cur_sim":"SIM1","signal":"0asu (-113dBm)","register":"Not registered","imsi":"","iccid":"","net_provider":"","net_type":"","plmnid":"","lac":"0","cellid":"0","imei":"862506049149928"},"network":{"status":"Disconnected","ip":"0.0.0.0","netmask":"0.0.0.0","gate":"0.0.0.0","dns":"0.0.0.0","time":"0 days, 00:00:00","speed":"RX: 1.13Kb, TX: 0b","ipv6":"::","gatev6":"::","dnsv6":"::"},"statistics":{"sim1":"RX: 0.0 MiB TX: 0.0 MiB ALL: 0.0 MiB","sim2":"RX: 180.4 MiB TX: 146.6 MiB ALL: 327.1 MiB"}}}]}]}

give me a minute

Replace the inject, with the output of the exec node.
I used inject to emulate the output of the exec

if you debug the output of the json node you will see how it is achieved.

[{"id":"be09724a5e928145","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"570c1643b0bde49f","type":"debug","z":"be09724a5e928145","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":160,"wires":[]},{"id":"d387737a4805a52b","type":"inject","z":"be09724a5e928145","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"id\":7,\"model\":\"UR32\",\"pn\":\"L04EU2211110FR0000000000\",\"oem\":\"0000\",\"rtver\":\"32.3.0.5\",\"status\":0,\"result\":[{\"get\":[{\"type\":\"yruo_cell_status\",\"index\":0,\"value\":{\"modem\":{\"modem_status\":\"No SIM Card\",\"model\":\"EC25\",\"version\":\"EC25EUXGAR08A03M1G\",\"cur_sim\":\"SIM1\",\"signal\":\"0asu (-113dBm)\",\"register\":\"Not registered\",\"imsi\":\"\",\"iccid\":\"\",\"net_provider\":\"\",\"net_type\":\"\",\"plmnid\":\"\",\"lac\":\"0\",\"cellid\":\"0\",\"imei\":\"862506049149928\"},\"network\":{\"status\":\"Disconnected\",\"ip\":\"0.0.0.0\",\"netmask\":\"0.0.0.0\",\"gate\":\"0.0.0.0\",\"dns\":\"0.0.0.0\",\"time\":\"0 days, 00:00:00\",\"speed\":\"RX: 1.13Kb, TX: 0b\",\"ipv6\":\"::\",\"gatev6\":\"::\",\"dnsv6\":\"::\"},\"statistics\":{\"sim1\":\"RX: 0.0 MiB TX: 0.0 MiB ALL: 0.0 MiB\",\"sim2\":\"RX: 180.4 MiB TX: 146.6 MiB ALL: 327.1 MiB\"}}}]}]}","payloadType":"str","x":130,"y":160,"wires":[["1a0f1b39ed29c446"]]},{"id":"1a0f1b39ed29c446","type":"json","z":"be09724a5e928145","name":"","property":"payload","action":"","pretty":false,"x":310,"y":160,"wires":[["7d91ea0434ed7c7a"]]},{"id":"7d91ea0434ed7c7a","type":"function","z":"be09724a5e928145","name":"","func":"const Value = msg.payload.result[0].get[0].value.statistics.sim2\nmsg.payload = Value.split(\":\")[3].trim()\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":160,"wires":[["570c1643b0bde49f"]]}]

There are other ways, but this is good NR learning for you :wink:
thankfully the output was a JSON string - so we could use a built in node to parse it

I replaced the inject, with the output of the exec node it works perfectly
I thank you infinitely :+1: :+1:

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