# HTTP post\_ node

**URL:** https://discourse.nodered.org/t/http-post-node/83029
**Category:** General
**Created:** [20 November 2023 10:54 UTC](https://discourse.nodered.org/t/http-post-node/83029 "2023-11-20T10:54:43Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Industry40](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Industry40](https://discourse.nodered.org/u/Industry40)
#### Post date: [20 November 2023 10:54 UTC](https://discourse.nodered.org/t/http-post-node/83029/1 "2023-11-20T10:54:43Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/d/ad41253710aba20fd674bb5bba281cefbba1a6a6.png)  
This is something I want to post from external client to server. Do someone have an example?

Thanks a lot for any reply

---

<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: [20 November 2023 11:00 UTC](https://discourse.nodered.org/t/http-post-node/83029/2 "2023-11-20T11:00:28Z")

</div>

> [@Industry40](#):
>
> I would like to know if its possible to make http request

Yes. The `http request` node does this.

> [@Industry40](#):
>
> someone have an example

There are hundreds of examples [in this forum](https://discourse.nodered.org/search?q=http%20request%20post%20json)

There is also the [cookbook](https://cookbook.nodered.org/#http-requests)

---

<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: [20 November 2023 11:02 UTC](https://discourse.nodered.org/t/http-post-node/83029/3 "2023-11-20T11:02:30Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [20 November 2023 11:19 UTC](https://discourse.nodered.org/t/http-post-node/83029/4 "2023-11-20T11:19:02Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Industry40](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Industry40](https://discourse.nodered.org/u/Industry40)
#### Post date: [20 November 2023 11:19 UTC](https://discourse.nodered.org/t/http-post-node/83029/5 "2023-11-20T11:19:50Z")

</div>

I want to post from node red.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [20 November 2023 11:21 UTC](https://discourse.nodered.org/t/http-post-node/83029/6 "2023-11-20T11:21:32Z")

</div>

In that case see Steve's post.

---

<div class="post-metadata">

### Author: ![Industry40](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Industry40](https://discourse.nodered.org/u/Industry40)
#### Post date: [20 November 2023 12:22 UTC](https://discourse.nodered.org/t/http-post-node/83029/7 "2023-11-20T12:22:00Z")

</div>

@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.

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

```

---

<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: [20 November 2023 12:26 UTC](https://discourse.nodered.org/t/http-post-node/83029/8 "2023-11-20T12:26:54Z")

</div>

> [@Industry40](#):
>
> an error says that header is not defined.

I assume you mean a squiggly red underline?

Your function starts with

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

```

you should make that

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

```

I guess it was a bad edit? (I would _never_ post bad code 😉 )

---

<div class="post-metadata">

### Author: ![Industry40](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Industry40](https://discourse.nodered.org/u/Industry40)
#### Post date: [20 November 2023 12:36 UTC](https://discourse.nodered.org/t/http-post-node/83029/9 "2023-11-20T12:36:26Z")

</div>

OH MY BAD. Thanks a lot.

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

---

<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: [20 November 2023 12:46 UTC](https://discourse.nodered.org/t/http-post-node/83029/10 "2023-11-20T12:46:38Z")

</div>

yes, you have likely deleted another important line.

put

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

---

<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 January 2024 12:47 UTC](https://discourse.nodered.org/t/http-post-node/83029/11 "2024-01-19T12:47:10Z")

</div>

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