# Translation of Curl command to HTTP Request

**URL:** <https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101>\
**Category:** General\
**Created:** [11 October 2021 18:30 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101 "2021-10-11T18:30:35Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [11 October 2021 18:30 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/1 "2021-10-11T18:30:35Z")

</div>

Hi,

Looking for some help, Trying to talk to a GL.iNet Router and having problems. I am just not grasping this - it should be so simple!!

I have been trying to convert a Curl command (which does work) to an HTTP Request for the last few days and am rapidly losing my fingerprints!

POST gets me a 200 response, but the password is not being read (among other diagnostics) "{"code":-1,"msg":"Token is null"}"

```auto
curl <IPADDRESS>/api/router/login -d "pwd=XXYYZZAABB"

```

Would really be grateful for some help.

TIA,  
Colin J

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [11 October 2021 21:37 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/2 "2021-10-11T21:37:36Z")

</div>

I know nothing but have always understood that curl cannot be replicated as a URL request/post/get

---

<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:** [11 October 2021 22:41 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/3 "2021-10-11T22:41:50Z")

</div>

What does this flow output?  
(NOTE: change the http/https and IP in the function as required)

```auto
[{"id":"4816c6bb62da7b02","type":"inject","z":"b872cb4b.5a6448","name":"Click me","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1260,"y":120,"wires":[["4816c6bb62da7b03"]]},{"id":"4816c6bb62da7b03","type":"function","z":"b872cb4b.5a6448","name":"","func":"//original curl: \n//\n//curl http://1.2.3.4/api/router/login -d \"pwd=XXYYZZAABB\"\n\nmsg.method = \"post\";\nmsg.url = \"http://1.2.3.4/api/router/login\";\nmsg.payload = `pwd=XXYYZZAABB`;\nmsg.headers = null;\nmsg.cookies = null;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1440,"y":120,"wires":[["4816c6bb62da7b04"]]},{"id":"4816c6bb62da7b04","type":"http request","z":"b872cb4b.5a6448","name":"","method":"use","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":1620,"y":120,"wires":[["4816c6bb62da7b05"]]},{"id":"4816c6bb62da7b05","type":"debug","z":"b872cb4b.5a6448","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1780,"y":120,"wires":[]}]

```

> [@mudwalker](#):
>
> POST gets me a 200 response, but the password is not being read (among other diagnostics) "{"code":-1,"msg":"Token is null"}"

Assuming curl works, this response suggests curl is sending more in the http request that you believe (something like a header or cookie that you will need to add to the msg for the node-red http request node). Do you have wireshark installed? Or perhaps postman? (something to fully inspect the request and the response outside of node-red)

Also, see these posts for similar / related tips and clues...

- [Web scraping with java Script - #4 by Steve-Mcl](https://discourse.nodered.org/t/web-scraping-with-java-script/44593/4)
- [Search on webpage and retrieve data - #3 by UnborN](https://discourse.nodered.org/t/search-on-webpage-and-retrieve-data/52091/3)

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [11 October 2021 22:57 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/4 "2021-10-11T22:57:34Z")

</div>

> [@Steve-Mcl](#):
>
> Do you have wireshark installed? Or perhaps postman? (something to fully inspect the request and the response outside of node-red)

[HTTP Toolkit](https://httptoolkit.tech/) ?

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [12 October 2021 06:25 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/5 "2021-10-12T06:25:41Z")

</div>

> [@Steve-Mcl](#):
>
> What does this flow output?

It is a request for a token to use with a REST API and the response comes in the form

```auto
{"code":0,"token":"1122334455667788aabbccddeeff"}

```

[Documentation](https://dev.gl-inet.com/api/)  
I had edited my curl call above to protect the innocent - the call is correctly constructed and does work. It is just that I am doing everything manually at the moment, each time I make the call, or reset the router, the token changes.

I have been looking in the Browser Developers Tools to see what is being sent, but can't quite get the complete picture and so will try the tools suggested. Wireshark overwhelms me for the very infrequent use I use it, so will try HTTP Toolkit first.

Other than that, I will try to use the function node to fetch the data.

I am just trying to understand what is going on and, after reading other posts, I see that the HTTP Request is suggested as an alternative to Curl in many instances. Perhaps, this can only be done by Curl.

Eventually, I hope to use Node-RED to monitor the signal strength direct off the GSM Modem to help me point the (directional) antenna in the right direction.

Thank you all for your help. It is greatly appreciated.

---

<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:** [12 October 2021 06:33 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/6 "2021-10-12T06:33:05Z")

</div>

> [@mudwalker](#):
>
> > [@Steve-Mcl](#):
> >
> > What does this flow output?
> 
> It is a request for a token to use with a REST API and the response comes in the form
> 
> ```auto
> {"code":0,"token":"1122334455667788aabbccddeeff"}
> 
> ```

Are you saying the flow I wrote gets you a code 0 and a token value?

If so, then I suspect you simply keep hold of the token and use that as a header in subsequent HTTP requests to other parts of the API.

Similar to the thread I linked to before.

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [12 October 2021 06:39 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/7 "2021-10-12T06:39:08Z")

</div>

This is the response from your code

```auto
payload: "{"msg":"password missing","code":-5}"

```

So, your code changes the response, but see that you have some other messages in there. Will look and analyse.

Just started to look at HTTP Toolkit to intercept Curl command and your flow.

EDIT: Looks like the headers may be the key and will try to adapt @Steve-Mcl flow to contain them. May take a while as I have other duties I need to do through the day.

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [12 October 2021 07:02 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/8 "2021-10-12T07:02:10Z")

</div>

Thank you Guys!!

Between @Steve-Mcl and @Andrei suggestions I have got this working, it is the Headers causing the problem

Changed the `msg.headers = null;` to `msg.headers = {'content-type':'application/x-www-form-urlencoded'}` and all now works!!

As usual the support from the forum is excellent.

I see @cymplecy suggests that 'curl cannot be replicated as a URL request/post/get'. Is this so for any particular circumstances?

Thank you again!

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [12 October 2021 07:07 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/9 "2021-10-12T07:07:34Z")

</div>

> [@mudwalker](#):
>
> I see @cymplecy suggests that 'curl cannot be replicated as a URL request/post/get'.

He also said that he knows nothing 🙂

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [12 October 2021 07:09 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/10 "2021-10-12T07:09:00Z")

</div>

🤣I saw that, but there is usually 'no smoke without a fire' so thought you may have come across an instance.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [12 October 2021 07:14 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/11 "2021-10-12T07:14:18Z")

</div>

Until today, I've never come across an instance where a curl could be replaced by something else

I don't understand http at all 🙂

---

<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:** [12 October 2021 08:02 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/12 "2021-10-12T08:02:17Z")

</div>

> [@mudwalker](#):
>
> I see @cymplecy suggests that 'curl cannot be replicated as a URL request/post/get'. Is this so for any particular circumstances

So far, I haven't come across any circumstances where I couldn't use the http request to replace curl. That said, curl has a LOT of options, I'm certain there will be something but I don't know what.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [12 October 2021 08:26 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/13 "2021-10-12T08:26:21Z")

</div>

Which bit of "I know nothing" are you lot having trouble with? 🙂  
Your arguing with an http idiot 🙂

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [14 October 2021 18:04 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/14 "2021-10-14T18:04:12Z")

</div>

So, managed to get the information I was after from my GL.iNET Router.

Below is a flow that gets the Security Token and then you can GET or POST a URL to the router to obtain the client list and cell information. If it is of use to anyone, well, here it is...

```auto
[
    {
        "id": "0f814aab11ca6a59",
        "type": "inject",
        "z": "362ac6b30a7e45bc",
        "name": "Click me",
        "props": [],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "str",
        "x": 140,
        "y": 320,
        "wires": [
            [
                "360590ca88c2741e"
            ]
        ]
    },
    {
        "id": "360590ca88c2741e",
        "type": "function",
        "z": "362ac6b30a7e45bc",
        "name": "Curl token",
        "func": "//original curl: \n//\n//curl http://1.2.3.4/api/router/login -d \"pwd=XXYYZZAABB\"\nmsg.headers = {}; \n\nmsg.method = \"post\";\nmsg.url = \"http://<IP_ADDRESS>/api/router/login\";\nmsg.payload = \"<PASSWORD>\";\nmsg.headers[\"Content-Type\"] = \"application/x-www-form-urlencoded\";\nmsg.cookies = null;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 370,
        "y": 320,
        "wires": [
            [
                "af82bb868566726f"
            ]
        ]
    },
    {
        "id": "af82bb868566726f",
        "type": "http request",
        "z": "362ac6b30a7e45bc",
        "name": "",
        "method": "use",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "credentials": {},
        "x": 550,
        "y": 320,
        "wires": [
            [
                "ea345df5628fa288"
            ]
        ]
    },
    {
        "id": "ea345df5628fa288",
        "type": "function",
        "z": "362ac6b30a7e45bc",
        "name": "global.curlToken",
        "func": "let curlToken = msg.payload;\nlet curlTest = msg.payload.slice(8, 9);\nmsg.test = curlTest;\n\nif (curlTest === \"0\"){\n curlToken = curlToken.slice(19, 51);\n global.set('curlToken', curlToken);\n msg.payload = \"Token OK\";\n} else {\n msg.payload = \"Token Error\";\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 740,
        "y": 320,
        "wires": [
            [
                "c402da04521e4baa"
            ]
        ]
    },
    {
        "id": "7fa01b1b8e12231b",
        "type": "http request",
        "z": "362ac6b30a7e45bc",
        "name": "",
        "method": "use",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "http://<IP_ADDRESS>/{{{topic}}}",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "credentials": {
            "user": "",
            "password": ""
        },
        "x": 550,
        "y": 360,
        "wires": [
            [
                "78aa4714ee584214"
            ]
        ]
    },
    {
        "id": "c402da04521e4baa",
        "type": "debug",
        "z": "362ac6b30a7e45bc",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1030,
        "y": 360,
        "wires": []
    },
    {
        "id": "3995f987df0f97bc",
        "type": "inject",
        "z": "362ac6b30a7e45bc",
        "name": "",
        "props": [
            {
                "p": "topic",
                "vt": "str"
            },
            {
                "p": "method",
                "v": "get",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "api/client/list",
        "x": 130,
        "y": 360,
        "wires": [
            [
                "ea6f81fcf7ea3965"
            ]
        ]
    },
    {
        "id": "ea6f81fcf7ea3965",
        "type": "function",
        "z": "362ac6b30a7e45bc",
        "name": "Curl token",
        "func": "msg.headers = {}; \n\nlet curlToken = global.get('curlToken');\n\nmsg.headers[\"Content-Type\"] = 'application/x-www-form-urlencoded';\nmsg.headers[\"Authorization\"] = curlToken;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 370,
        "y": 360,
        "wires": [
            [
                "7fa01b1b8e12231b"
            ]
        ]
    },
    {
        "id": "c4fb67c8a0f07f31",
        "type": "function",
        "z": "362ac6b30a7e45bc",
        "name": "Curl token",
        "func": "msg.headers = {}; \n\nlet curlToken = global.get('curlToken');\n\nmsg.headers[\"Content-Type\"] = \"application/x-www-form-urlencoded\";\nmsg.headers[\"Authorization\"] = curlToken;\nmsg.payload = \"modem_id=1&bus=1-1.2\";\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 370,
        "y": 400,
        "wires": [
            [
                "1220b9f446f8bb67"
            ]
        ]
    },
    {
        "id": "e09431020a17d4eb",
        "type": "inject",
        "z": "362ac6b30a7e45bc",
        "name": "",
        "props": [
            {
                "p": "topic",
                "vt": "str"
            },
            {
                "p": "method",
                "v": "post",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "api/modem/cells/get",
        "x": 160,
        "y": 400,
        "wires": [
            [
                "c4fb67c8a0f07f31"
            ]
        ]
    },
    {
        "id": "78aa4714ee584214",
        "type": "json",
        "z": "362ac6b30a7e45bc",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 710,
        "y": 360,
        "wires": [
            [
                "c402da04521e4baa"
            ]
        ]
    },
    {
        "id": "1220b9f446f8bb67",
        "type": "http request",
        "z": "362ac6b30a7e45bc",
        "name": "",
        "method": "use",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "http://<IP_ADDRESS>/{{{topic}}}",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "credentials": {
            "user": "",
            "password": ""
        },
        "x": 550,
        "y": 400,
        "wires": [
            [
                "fcd29dc0e286e61e"
            ]
        ]
    },
    {
        "id": "fcd29dc0e286e61e",
        "type": "json",
        "z": "362ac6b30a7e45bc",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 710,
        "y": 400,
        "wires": [
            [
                "9c0b88b96b6039d3"
            ]
        ]
    },
    {
        "id": "9c0b88b96b6039d3",
        "type": "function",
        "z": "362ac6b30a7e45bc",
        "name": "",
        "func": "let rssi = msg.payload.cells[0].rssi\nmsg.payload = msg.payload.cells[0].rssi;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 860,
        "y": 400,
        "wires": [
            [
                "c402da04521e4baa"
            ]
        ]
    }
]

```

Thank you everyone for your help!

EDIT: Modified Flow with thanks to @UnborN and @E1cid for making it easier and less convoluted!!

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [14 October 2021 21:42 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/15 "2021-10-14T21:42:41Z")

</div>

Hi .. thumbs up for finding a solution 👍  
but what are those unnecessary code you are doing in your "Load Headers" Functions ?  
(creating a JSON string with escaping double quotes, concatenation of strings and json.parse) 😇

you can simply do

```auto
msg.headers["Content-Type"] = "application/x-www-form-urlencoded";
msg.headers["Authorization"] = curlToken;

```

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [15 October 2021 16:43 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/16 "2021-10-15T16:43:34Z")

</div>

Tried this and I get `"TypeError: Cannot set property 'Content-Type' of undefined" `. I will have a play as that looks a lot easier if I can get it to work.

Spent many hours on my convoluted solution, couldn't get the curlToken into the Authorization: header.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [15 October 2021 16:56 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/17 "2021-10-15T16:56:55Z")

</div>

you need to declare msg.headers as an object  
add

```auto
msg.headers = {}; 

```

before the other references to msg.headers.

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [15 October 2021 17:00 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/18 "2021-10-15T17:00:06Z")

</div>

> [@E1cid](#):
>
> `msg.headers = {}; `

Oh WOW! Thank you!! It is so easy when you know how!!

Not sure I would ever have sussed that out!!

Modified flow posted above.

---

<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:** [29 October 2021 17:00 UTC](https://discourse.nodered.org/t/translation-of-curl-command-to-http-request/52101/19 "2021-10-29T17:00:35Z")

</div>

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