HTTP post_ node

Hello,

Quite new to node red. I would like to know if its possible to make http request to post commands from a client server to host.

HTTP
This is something I want to post from external client to server. Do someone have an example?

Thanks a lot for any reply

Yes. The http request node does this.

There are hundreds of examples in this forum

There is also the cookbook

PS, please dont post pictures of code.

It is preferable to post code in a code block

```
   paste code as text inside triple `back-ticks`
```

Do you mean you want to post to node-red or from node-red.

If you want to post to node red then I think you need to use http in and http response nodes.

I want to post from node red.

In that case see Steve's post.

@Steve-Mcl Hi,
I have seen you explanation in another post and created a similar function as shown below. But it shows an error says that header is not defined.

[
    {
        "id": "f5f002edd615ee59",
        "type": "http request",
        "z": "c4c83788d56b12ab",
        "name": "",
        "method": "use",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 730,
        "y": 720,
        "wires": [
            []
        ]
    },
    {
        "id": "7ae690eff112b561",
        "type": "inject",
        "z": "c4c83788d56b12ab",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 160,
        "y": 720,
        "wires": [
            [
                "17809da405f22d1d"
            ]
        ]
    },
    {
        "id": "17809da405f22d1d",
        "type": "function",
        "z": "c4c83788d56b12ab",
        "name": "",
        "func": "headers[\"Content-Type\"] = \"application/json\"\nmsg.method = \"POST\"\nmsg.headers = headers\nmsg.url = \"http://ip-adress:8100/order\"\nmsg.payload = {\"id\": 1000000100000000000,\"action\":[{\"action\":\"drive\",\"target\":{\"coordinate\":[{\n          \"x\": 48.125,\n          \"y\": 95.461,\n          \"angle\": 0\n        }]\n}\n}]\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 440,
        "y": 720,
        "wires": [
            [
                "f5f002edd615ee59"
            ]
        ]
    }
]

I assume you mean a squiggly red underline?

Your function starts with

headers["Content-Type"] = "application/json"

you should make that

msg.headers["Content-Type"] = "application/json"

I guess it was a bad edit? (I would never post bad code :wink: )

1 Like

OH MY BAD. Thanks a lot.

TypeError: Cannot set properties of undefined (setting 'Content-Type')
Also any idea what the above error means?

yes, you have likely deleted another important line.

put

msg.headers = msg.headers || {}

at the top of the function.

Quick explanation

msg.headers["Content-Type"] = "application/json" means "add a property named "Content-Type" to the msg.headers object)

If msg.headers is not an object you will get the error Cannot set properties of undefined (setting 'Content-Type')

So what the new line of code means is set msg.headers to its self OR (||) if it is null/empty, set it to a NEW object {}

Once msg.headers IS and object ({}) you can add properties to it.

Advise

Forgive me for saying but this is fairly base level JavaScript knowledge. You would do well to read up on basic level JavaScript. Especially about objects and properties etc. You can avoid this if you avoid the function node, but even a little knowledge will help you understand the whole msg thing that node-red depends on.

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