Using Node-Red instead of curl for a api request

I came across node-red with a flow which is reading out charging values from my SMA wallbox. The flow was pre configured. From that starting point I played around a lot, dove into node-red, looked at all parts of the flow, expanded and personalized the flow and actually produced something very useful for me.

There is still one thing I don't understand, I did not manage to translate the curl command into an native node-red flow.

Here is the working curl command including verbose output, the bearer token is .

curl -v http://192.168.47.82/api/v1/measurements/live/ -d "[{\"componentId\":\"IGULD:SELF\"}]" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2OTkxOTMxNDgsInN1YiI6ImFkbWluIiwidWlkIjoiMDI5N2FhMjMtODI1YS00ZGUzLWFiYzQtMzQ1Y2U5ODBhZDQ3IiwiZXhwIjoxNjk5MTk2NzQ4fQ.6vTTw9Eid4iLWqIdMrBvmbSODiz2ApclVzrgYA3Yryo"

and the verbose output

*   Trying 192.168.47.82:80...
* Connected to 192.168.47.82 (192.168.47.82) port 80 (#0)
> POST /api/v1/measurements/live/ HTTP/1.1
> Host: 192.168.47.82
> User-Agent: curl/8.0.1
> Accept: */*
> Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2OTkxOTMxNDgsInN1YiI6ImFkbWluIiwidWlkIjoiMDI5N2FhMjMtODI1YS00ZGUzLWFiYzQtMzQ1Y2U5ODBhZDQ3IiwiZXhwIjoxNjk5MTk2NzQ4fQ.6vTTw9Eid4iLWqIdMrBvmbSODiz2ApclVzrgYA3Yryo
> Content-Length: 30
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 200 OK
< Date: Sun, 05 Nov 2023 14:06:08 GMT
< Cache-Control: no-cache,no-store,must-revalidate
< Content-Type: application/json
< Transfer-Encoding: chunked

[{"channelId":"Measurement.ChaSess.WhIn", ....
...
...
here comes the requested data

Here is the flow I would like to use to replace it, I tried many different settings, return is either a "not logged" in message or a "500 Server error"

[
    {
        "id": "6ef9a42282b67566",
        "type": "inject",
        "z": "fce05b304dab0b49",
        "name": "Ausloeser",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "true",
        "payloadType": "bool",
        "x": 160,
        "y": 840,
        "wires": [
            [
                "node1"
            ]
        ]
    },
    {
        "id": "node1",
        "type": "function",
        "z": "fce05b304dab0b49",
        "name": "Daten vorbereiten",
        "func": "var Befehl = {'componentId':'IGULD:SELF'};\nmsg.headers = {};\nmsg.headers = {\n    'User-Agent': 'curl/8.0.1',\n    'Accept': '*/*',\n    'Authorization': 'Bearer ' + flow.get(\"wallbox_data_retrieve_token\"),\n    'Content-Type': 'application/x-www-form-urlencoded'\n};\nmsg.payload = Befehl;\nmsg.url = \"http://192.168.47.82/api/v1/measurements/live/\";\nmsg.method = \"POST\";\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 370,
        "y": 840,
        "wires": [
            [
                "node2",
                "89537487970d119e"
            ]
        ]
    },
    {
        "id": "node2",
        "type": "http request",
        "z": "fce05b304dab0b49",
        "name": "HTTP POST-Anfrage",
        "method": "use",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 600,
        "y": 840,
        "wires": [
            [
                "node3"
            ]
        ]
    },
    {
        "id": "89537487970d119e",
        "type": "debug",
        "z": "fce05b304dab0b49",
        "name": "Post Daten",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 550,
        "y": 760,
        "wires": []
    },
    {
        "id": "node3",
        "type": "debug",
        "z": "fce05b304dab0b49",
        "name": "Rueckgabe Wallbox",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 840,
        "y": 840,
        "wires": []
    }
]

This won't help. You need the method to be POST.

Hi,

I set the method in the function node. In the http node I chose set by msg.method althoug when I started to play arround I tried to use the HTTP node only. Here is the pure function code.

msg.headers = {};
msg.headers = {
    'User-Agent': 'curl/8.0.1',
    'Accept': '*/*',
    'Authorization': 'Bearer ' + flow.get("wallbox_data_retrieve_token"),
    'Content-Type': 'application/x-www-form-urlencoded'
};
msg.payload = Befehl;
msg.url = "http://192.168.47.82/api/v1/measurements/live/";
msg.method = "POST";
return msg;```

Not certain why you are setting 'Content-Type': 'application/x-www-form-urlencoded'

Try this flow does it work?

[{"id":"463a0cb6fdb45767","type":"inject","z":"6e46a349d744ceaa","name":"Click me","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":440,"y":380,"wires":[["590a60b220fd6a95"]]},{"id":"590a60b220fd6a95","type":"function","z":"6e46a349d744ceaa","name":"Prepare Req","func":"// original curl:\n// curl -v http://192.168.47.82/api/v1/measurements/live/ -d \"[{\\\"componentId\\\":\\\"IGULD:SELF\\\"}]\" -H \"Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2OTkxOTMxNDgsInN1YiI6ImFkbWluIiwidWlkIjoiMDI5N2FhMjMtODI1YS00ZGUzLWFiYzQtMzQ1Y2U5ODBhZDQ3IiwiZXhwIjoxNjk5MTk2NzQ4fQ.6vTTw9Eid4iLWqIdMrBvmbSODiz2ApclVzrgYA3Yryo\"\n// \n\n// msg.method = \"post\" // set in HTTP Request node\n// msg.url = \"http://192.168.47.82/api/v1/measurements/live/\" // set in HTTP Request node\nmsg.payload = [\n    {\n        \"componentId\":\"IGULD:SELF\"\n    }\n]\nmsg.headers = {\n    \"Authorization\": \"Bearer eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2OTkxOTMxNDgsInN1YiI6ImFkbWluIiwidWlkIjoiMDI5N2FhMjMtODI1YS00ZGUzLWFiYzQtMzQ1Y2U5ODBhZDQ3IiwiZXhwIjoxNjk5MTk2NzQ4fQ.6vTTw9Eid4iLWqIdMrBvmbSODiz2ApclVzrgYA3Yryo\"\n}\n\nreturn msg","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":380,"wires":[["17a81a0ab2d7c413"]]},{"id":"17a81a0ab2d7c413","type":"http request","z":"6e46a349d744ceaa","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"http://192.168.47.82/api/v1/measurements/live/","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":780,"y":380,"wires":[["dae7d7d4e03c31f1"]]},{"id":"dae7d7d4e03c31f1","type":"debug","z":"6e46a349d744ceaa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":960,"y":380,"wires":[]}]

It works! But this is pretty much where i started. What ever I missed,... I set the header because in the curl output this was shown. Thanks a lot

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