# Using Node-Red instead of curl for a api request

**URL:** <https://discourse.nodered.org/t/using-node-red-instead-of-curl-for-a-api-request/82623>\
**Category:** General\
**Created:** [5 November 2023 14:16 UTC](https://discourse.nodered.org/t/using-node-red-instead-of-curl-for-a-api-request/82623 "2023-11-05T14:16:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![seballa](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@seballa](https://discourse.nodered.org/u/seballa)\
**Post date:** [5 November 2023 14:16 UTC](https://discourse.nodered.org/t/using-node-red-instead-of-curl-for-a-api-request/82623/1 "2023-11-05T14:16:07Z")

</div>

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 .

```auto
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

```auto
* 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"

```auto
[
    {
        "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": []
    }
]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [5 November 2023 14:19 UTC](https://discourse.nodered.org/t/using-node-red-instead-of-curl-for-a-api-request/82623/2 "2023-11-05T14:19:59Z")

</div>

> [@seballa](#):
>
> `"method": "use",`

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

---

<div class="post-metadata">

**Author:** ![seballa](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@seballa](https://discourse.nodered.org/u/seballa)\
**Post date:** [5 November 2023 15:00 UTC](https://discourse.nodered.org/t/using-node-red-instead-of-curl-for-a-api-request/82623/3 "2023-11-05T15:00:59Z")

</div>

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.

````plaintext
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;```
````

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [5 November 2023 15:16 UTC](https://discourse.nodered.org/t/using-node-red-instead-of-curl-for-a-api-request/82623/4 "2023-11-05T15:16:05Z")

</div>

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

Try this flow does it work?

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![seballa](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@seballa](https://discourse.nodered.org/u/seballa)\
**Post date:** [5 November 2023 15:21 UTC](https://discourse.nodered.org/t/using-node-red-instead-of-curl-for-a-api-request/82623/5 "2023-11-05T15:21:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [19 November 2023 15:22 UTC](https://discourse.nodered.org/t/using-node-red-instead-of-curl-for-a-api-request/82623/6 "2023-11-19T15:22:46Z")

</div>

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