# Global context variable in http request headers?

**URL:** <https://discourse.nodered.org/t/global-context-variable-in-http-request-headers/91246>\
**Category:** General\
**Created:** [25 September 2024 08:46 UTC](https://discourse.nodered.org/t/global-context-variable-in-http-request-headers/91246 "2024-09-25T08:46:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [25 September 2024 08:46 UTC](https://discourse.nodered.org/t/global-context-variable-in-http-request-headers/91246/1 "2024-09-25T08:46:24Z")

</div>

Is it possible to use context variables in http request header fields?

Like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/3480c7189acdd490ee27c0b05817a0338b0be907.png)

Because I can't get it to work (results in 401 unauthorized). Instead, I add msg.token in a function node before http request to get the token from global context.

I thought all fields could take context variables? Can HTTP request headers take them?

---

<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:** [25 September 2024 09:36 UTC](https://discourse.nodered.org/t/global-context-variable-in-http-request-headers/91246/2 "2024-09-25T09:36:31Z")

</div>

> [@ThingsTinkerer](#):
>
> Is it possible to use context variables in http request header fields?

Yes. several ways. Easiest is use a change node BEFORE the http request and set `msg.xxx` to `global.yyy` then set the header option to `msg.` and enter `xxx` (use better names)

Alternatively, dont add any headers in the dialog and set them up in a function node

```auto
msg.headers = {
   "my-header": global.get("my-value")
}
return msg

```

---

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [25 September 2024 10:10 UTC](https://discourse.nodered.org/t/global-context-variable-in-http-request-headers/91246/3 "2024-09-25T10:10:19Z")

</div>

Yeah that's what I've done, using the function node beforehand. So no way of using context variables directly? I thought this was a feature in some nodes.

msg.token = global.get("TOKEN");  
return msg;

Then add msg.token to header field in http request node.

This is extra node and extra code to extract the value, instead of simply using it directly in the http request node. Hoped there was a way to do this with less code (less code is usually better code) 😉

Ideally, the http request node would have an option like this:  
 ![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/d/4d9c686ed85e4e2598d861e77479991c948238d2.png)  
source:  
[Variables in edit fields - General - Node-RED Forum (nodered.org)](https://discourse.nodered.org/t/variables-in-edit-fields/83281)

---

<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:** [25 September 2024 10:18 UTC](https://discourse.nodered.org/t/global-context-variable-in-http-request-headers/91246/4 "2024-09-25T10:18:17Z")

</div>

> [@ThingsTinkerer](#):
>
> So no way of using context variables directly? I thought this was a feature in some nodes.

In some nodes yes. When [this feature was added](https://github.com/node-red/node-red/issues/3428), it was decided not to support anything other than `msg.`

> [@ThingsTinkerer](#):
>
> Ideally, the http request node would have an option like this

I am on the fence here. Historically, I would have said "yeah, lets add every option, cos options are good" however, I am slowly learning that presenting too many options is not always the best course of action. As it stands, this is a very clean interface as it is and TBH, it is not terribly difficult to prepare your headers atomically in a node beforehand.

PS, by "I am on the fence here" I mean - if lots of folk request this, I personally wouldnt block more things being added to the typed input on the RH side on this node.

---

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [25 September 2024 10:20 UTC](https://discourse.nodered.org/t/global-context-variable-in-http-request-headers/91246/5 "2024-09-25T10:20:11Z")

</div>

Yeah I guess the downsides is presenting all the options in GUI. Because in code you always have all these options available. So it's a matter between more GUI options vs more nodes/code.

---

<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:** [9 October 2024 10:21 UTC](https://discourse.nodered.org/t/global-context-variable-in-http-request-headers/91246/6 "2024-10-09T10:21:09Z")

</div>

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