# Sending the TOKEN seperate of the block

**URL:** <https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799>\
**Category:** General\
**Created:** [22 February 2022 09:10 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799 "2022-02-22T09:10:06Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Milanovic](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Milanovic](https://discourse.nodered.org/u/Milanovic)\
**Post date:** [22 February 2022 09:10 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/1 "2022-02-22T09:10:07Z")

</div>

Hi,

I'm reading out some data with API, now I want to read different values but that will say that I need different tokens. I would ask how I can send the token I want to use. So if I didn't fill anything in the part  
'Token', is there is possibility to fill it in with an extern block like inject?

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

---

<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:** [22 February 2022 09:38 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/2 "2022-02-22T09:38:51Z")

</div>

Have you tried sending the authorization in the headers

```auto
Authorization: Bearer {token}

```

---

<div class="post-metadata">

**Author:** ![Milanovic](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Milanovic](https://discourse.nodered.org/u/Milanovic)\
**Post date:** [22 February 2022 13:10 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/3 "2022-02-22T13:10:31Z")

</div>

@E1cid

Dp you mean to set this in the timestamp? If I try this it didn't work

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/8776610ec2747c90289bc658a2bba9efdbd7c2a4.png)

---

<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:** [22 February 2022 13:26 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/4 "2022-02-22T13:26:07Z")

</div>

No set a msg.headers object prior to the http request.  
`{"Authorization": "Bearer " + token}`  
You would need to base64 encode the token if it is not already.

[edit]  
example of setting msg.headers.Authorization using a change node

```auto
[{"id":"f9316e19.7381e","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"token","v":"sdfgh345666","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":120,"y":1160,"wires":[["7387c36c.54496c"]]},{"id":"7387c36c.54496c","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"headers.Authorization","pt":"msg","to":"\"Bearer \" & $base64encode($$.token)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":1140,"wires":[["7943a6c5.b78e4"]]},{"id":"7943a6c5.b78e4","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":680,"y":1160,"wires":[]}]

```

---

<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:** [22 February 2022 13:28 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/5 "2022-02-22T13:28:01Z")

</div>

> [@Milanovic](#):
>
> Dp you mean to set this in the timestamp? If I try this it didn't work

No, @E1cid means have you set the incoming msg.headers to an object containing `Authorization: Bearer {token}` (where `token` is your token)

e.g.

- turn off "Use Authentication"
- in a change node or function node (before the HTTP Request)
  - `msg.headers = {Authorization: "Bearer " + msg.token}}; `

(where `msg.token` is a base64 encoded token)

---

<div class="post-metadata">

**Author:** ![Milanovic](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Milanovic](https://discourse.nodered.org/u/Milanovic)\
**Post date:** [23 February 2022 09:54 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/6 "2022-02-23T09:54:24Z")

</div>

I don't understand how I can do this, which nodes does I need?

---

<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:** [23 February 2022 09:56 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/7 "2022-02-23T09:56:54Z")

</div>

> [@Milanovic](#):
>
> I don't understand how I can do this

Did you try the example @E1cid kindly provided?

> [@E1cid](#):
>
> example of setting msg.headers.Authorization using a change node
> 
> ```auto
> [{"id":"f9316e19.7381e","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"token","v":"sdfgh345666","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":120,"y":1160,"wires":[["7387c36c.54496c"]]},{"id":"7387c36c.54496c","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"headers.Authorization","pt":"msg","to":"\"Bearer \" & $base64encode($$.token)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":1140,"wires":[["7943a6c5.b78e4"]]},{"id":"7943a6c5.b78e4","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":680,"y":1160,"wires":[]}]
> 
> ```

---

<div class="post-metadata">

**Author:** ![Milanovic](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Milanovic](https://discourse.nodered.org/u/Milanovic)\
**Post date:** [23 February 2022 10:01 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/8 "2022-02-23T10:01:41Z")

</div>

```auto
[{"id":"132a311fdb8354c4","type":"http request","z":"c3c9206917830458","name":"Switch sturen [1]","method":"POST","ret":"obj","paytoqs":"body","url":"https://app1pub.smappee.net/dev/v3/servicelocation/59792/actuator/1/on","tls":"","persist":false,"proxy":"","authType":"","senderr":true,"x":460,"y":340,"wires":[["0568392c58ab7aa4"]]},{"id":"0568392c58ab7aa4","type":"debug","z":"c3c9206917830458","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":340,"wires":[]},{"id":"9cd6ac5162824796","type":"inject","z":"c3c9206917830458","name":"Switch ON","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"duration\": 300}","payloadType":"json","x":180,"y":340,"wires":[["132a311fdb8354c4"]]},{"id":"3cca0cbf31139cd5","type":"http request","z":"c3c9206917830458","name":"Switch sturen [0]","method":"POST","ret":"obj","paytoqs":"body","url":"https://app1pub.smappee.net/dev/v3/servicelocation/59792/actuator/1/off","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":460,"y":380,"wires":[["5d11ae9ab9d89b4f"]]},{"id":"5d11ae9ab9d89b4f","type":"debug","z":"c3c9206917830458","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":380,"wires":[]},{"id":"7f723fe72df17ab1","type":"inject","z":"c3c9206917830458","name":"Switch OFF","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"duration\": 300}","payloadType":"json","x":170,"y":380,"wires":[["3cca0cbf31139cd5"]]},{"id":"72db6a0f32dffb78","type":"comment","z":"c3c9206917830458","name":"info switch on/off","info":"switch on/off, hiermee kunnen we de switch gaan aan- en uitsturen. We sturen zelf de duration mee, daarnaast moeten we nog de on/off gaan meesturen indien we het willen vanuit één blok doen","x":460,"y":300,"wires":[]},{"id":"f9316e19.7381e","type":"inject","z":"c3c9206917830458","name":"","props":[{"p":"token","v":"a733c03b-a21e-3d95-94f1-ecf782696280","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":500,"wires":[["7387c36c.54496c"]]},{"id":"7387c36c.54496c","type":"change","z":"c3c9206917830458","name":"","rules":[{"t":"set","p":"headers.Authorization","pt":"msg","to":"\"Bearer \" & $base64encode($$.token)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":480,"wires":[["132a311fdb8354c4","3cca0cbf31139cd5"]]}]

```

Yes I tried this, but it didn't work  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/8/98483afca5f1cf15630b8cdbac44cf833c78e13d.png)

---

<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:** [23 February 2022 11:02 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/9 "2022-02-23T11:02:19Z")

</div>

Appears your authorization is already encodeded  
this return no errors for me

```auto
[{"id":"47742e80.4d9b5","type":"inject","z":"bf9e1e33.030598","name":"Switch ON","props":[{"p":"payload"},{"p":"token","v":"a733c03b-a21e-3d95-94f1-ecf782696280","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"duration\": 300}","payloadType":"json","x":100,"y":1720,"wires":[["5e386841.b5165"]]},{"id":"5e386841.b5165","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"headers.Authorization","pt":"msg","to":"\"Bearer \" & $$.token","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":1740,"wires":[["ade26476.383b"]]},{"id":"ade26476.383b","type":"http request","z":"bf9e1e33.030598","name":"Switch sturen [1]","method":"POST","ret":"txt","paytoqs":"body","url":"https://app1pub.smappee.net/dev/v3/servicelocation/59792/actuator/1/on","tls":"","persist":false,"proxy":"","authType":"","x":420,"y":1800,"wires":[["baf21866.52c91"]]},{"id":"ccbdbaea.cbce8","type":"inject","z":"bf9e1e33.030598","name":"Switch OFF","props":[{"p":"payload"},{"p":"token","v":"a733c03b-a21e-3d95-94f1-ecf782696280","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"duration\": 300}","payloadType":"json","x":140,"y":1780,"wires":[["5e386841.b5165"]]},{"id":"baf21866.52c91","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":1720,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Milanovic](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Milanovic](https://discourse.nodered.org/u/Milanovic)\
**Post date:** [23 February 2022 12:17 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/10 "2022-02-23T12:17:07Z")

</div>

Yes, indeed it works!

But the next question, is it possible to set different tokens? maybe with a variable.

---

<div class="post-metadata">

**Author:** ![Milanovic](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Milanovic](https://discourse.nodered.org/u/Milanovic)\
**Post date:** [23 February 2022 14:23 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/11 "2022-02-23T14:23:20Z")

</div>

So what I mean is this:  
If I have a global variable, I can change it whenever I want with a text field. But how can I set the global variable in the inject node?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/221be3e5c0ade6e2c3c9ea13ea1dc488a313f4c3.png)

---

<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:** [23 February 2022 14:46 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/12 "2022-02-23T14:46:35Z")

</div>

That would be just `token`, but why pay the middleman, when you can set the global in the change node

```auto
[{"id":"815dc892.8a7f08","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"headers.Authorization","pt":"msg","to":"\"Bearer \" & $globalContext(\"token\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":1980,"wires":[["8fcd946a.13cff8"]]}]

```

```auto
"Bearer " & $globalContext("token")

```

Time to mark a solution to help others find answers, If you have new questions start a new topic.

---

<div class="post-metadata">

**Author:** ![Milanovic](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Milanovic](https://discourse.nodered.org/u/Milanovic)\
**Post date:** [25 February 2022 10:11 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/13 "2022-02-25T10:11:07Z")

</div>

If I wtry to use the UserMatrix[6][1] it doesn't work..

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/7/1763a51a1164b6fa08ed8604fcd08ca099c06e40.png)

---

<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 February 2022 10:13 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/14 "2022-02-25T10:13:51Z")

</div>

Look again at the example @E1cid provided. You have the JSONata syntax wrong.

---

<div class="post-metadata">

**Author:** ![Milanovic](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Milanovic](https://discourse.nodered.org/u/Milanovic)\
**Post date:** [25 February 2022 10:14 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/15 "2022-02-25T10:14:16Z")

</div>

How do you mean?

---

<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 February 2022 10:19 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/16 "2022-02-25T10:19:36Z")

</div>

Can't you see where the value you want to use would go if you followed the provided example ...

> [@E1cid](#):
>
> `"Bearer " & $globalContext("token")`

E.g...

```auto
"Bearer " & $globalContext("some-other-named-global[1][2]")

```

---

<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 February 2022 10:30 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/17 "2022-02-25T10:30:07Z")

</div>

In fact, @E1cid gave you the answer here: [URL outside of the http request - #2 by E1cid](https://discourse.nodered.org/t/url-outside-of-the-http-request/58970/2)

Please dont cross post - it just gets messy.

---

<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:** [26 April 2022 10:30 UTC](https://discourse.nodered.org/t/sending-the-token-seperate-of-the-block/58799/18 "2022-04-26T10:30:22Z")

</div>

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