Use the HTTP-Request to recreate an curl command

Hello, i am trying since 2h and cant find an working solution.

I try to build the following command with the nodered http-request:

curl -d "otpType=ReadRealTimeData&pwd=PASSWD" -X POST http://5.8.8.8

Can anybody help me?

Try setting the http request node to
method POST
url http://5.8.8.8
and feed in payload

{
"otpType": "ReadRealTimeData",
"pwd": "PASSWD"
}

Also need to set the Content-Type header to application/x-www-form-urlencoded, as it will format it out as JSON I believe

[
    {
        "id": "e3d6e970.e7768",
        "type": "inject",
        "z": "581fc531.7fd2ac",
        "name": "",
        "props": [],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 130,
        "y": 860,
        "wires": [
            [
                "fb96465e77051bc0"
            ]
        ]
    },
    {
        "id": "c62fdf0a.1924d8",
        "type": "http request",
        "z": "581fc531.7fd2ac",
        "name": "",
        "method": "POST",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "http://5.8.8.8",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [
            {
                "keyType": "Content-Type",
                "keyValue": "",
                "valueType": "other",
                "valueValue": "application/x-www-form-urlencoded"
            }
        ],
        "x": 470,
        "y": 860,
        "wires": [
            [
                "af8c089c.a6fbe8"
            ]
        ]
    },
    {
        "id": "af8c089c.a6fbe8",
        "type": "debug",
        "z": "581fc531.7fd2ac",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 690,
        "y": 860,
        "wires": []
    },
    {
        "id": "fb96465e77051bc0",
        "type": "function",
        "z": "581fc531.7fd2ac",
        "name": "",
        "func": "msg.payload = {\n    \"otpType\": \"ReadRealTimeData\",\n    \"pwd\": \"PASSWD\"\n};\n\nreturn msg\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 280,
        "y": 860,
        "wires": [
            [
                "c62fdf0a.1924d8"
            ]
        ]
    }
]

This is my first try. But i am getting an error:

"RequestError: socket hang up : http://5.8.8.8"

Try unchecking basic authorization in the http request node

Corrected the function Node to:

msg.headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
};


msg.payload = {
    "otpType": "ReadRealTimeData",
    "pwd": "SXK2PXZVEK"
};

return msg

But the error is the same.

basic auth. is not set.

This works using curl on the same machine as Node RED right?

scratch that must of accidently checked it myself

yes it works:

curl -d "optType=ReadRealTimeData&pwd=PASSWD" -X POST http://5.8.8.8
{"sn":"*****","ver":"3.003.02","type":7,"Data":[2321,2331,2332,39,39,40,894,890,919,3307,4459,36,36,1181,1576,5000,5000,5000,2,4573,0,71,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,8,8000,0,29,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,33,0,0,0,0,0,0,0,2679,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"Information":[8.000,7,"****",8,1.22,1.02,1.33,1.02,0.00,1]

no problem, i am happy when someone is trying to help me. I am sitting since hours on this simple task :frowning:

Just for fun what happens if you enable keep alive?

image

I currently cant see any difference between curl and Node RED

The headers differences are here
the top object is the curl from command line, and lower is a http request from node-red

Yes i captured the HTTP-Headers via tcpdump ant tried to reconfigure the function. My result:

Curl- HTTP-Request

POST / HTTP/1.1
Host: 5.8.8.8
User-Agent: curl/7.74.0
Accept: */*
Content-Length: 39
Content-Type: application/x-www-form-urlencoded

optType=ReadRealTimeData&pwd=PASSWD [|http]

Nodered- HTTP-Request

POST / HTTP/1.1
User-Agent: nodered
Accept: */*
Content-Length: 39
Content-Type: application/x-www-form-urlencoded
Host: 5.8.8.8
Connection: keep-alive

otpType=ReadRealTimeData&pwd=PASSWD [|http]

I am still getting an error.

vs.

?

"opt" vs "otp" ? :slight_smile:

1 Like

:rofl: that was the error. I think a should take a brake now :grinning:

2 Likes

This is the result, thank you all for your time ! :grinning:

[
    {
        "id": "6f3b89af5e9fde75",
        "type": "inject",
        "z": "cc21c9460c41635a",
        "name": "",
        "props": [],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 130,
        "y": 960,
        "wires": [
            [
                "aa80346ea031c53f"
            ]
        ]
    },
    {
        "id": "64b0442cd898bef8",
        "type": "http request",
        "z": "cc21c9460c41635a",
        "name": "",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "5.8.8.8",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [
            {
                "keyType": "User-Agent",
                "keyValue": "",
                "valueType": "other",
                "valueValue": "NodeRed"
            },
            {
                "keyType": "Accept-Encoding",
                "keyValue": "",
                "valueType": "other",
                "valueValue": "*/*"
            },
            {
                "keyType": "Content-Type",
                "keyValue": "",
                "valueType": "other",
                "valueValue": "application/x-www-form-urlencoded"
            }
        ],
        "x": 490,
        "y": 960,
        "wires": [
            [
                "399b9fe458f69ad0"
            ]
        ]
    },
    {
        "id": "399b9fe458f69ad0",
        "type": "debug",
        "z": "cc21c9460c41635a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 690,
        "y": 960,
        "wires": []
    },
    {
        "id": "aa80346ea031c53f",
        "type": "function",
        "z": "cc21c9460c41635a",
        "name": "",
        "func": "msg.payload = {};\nmsg.payload = {\n    \"optType\": \"ReadRealTimeData\",\n    \"pwd\": \"PASSWD\"\n};\n\nreturn msg\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 300,
        "y": 960,
        "wires": [
            [
                "64b0442cd898bef8"
            ]
        ]
    }
]

Well spotted @cameo69

@SteveRed
Can you mark @cameo69's post as the answer - it will remind us to pay more attention in future :grinning:

Done. Thank you, this is a good forum. Hope a can help next time an other user. Kind regards.

3 Likes

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