# Payload to base64

**URL:** <https://discourse.nodered.org/t/payload-to-base64/61447>\
**Category:** General\
**Created:** [18 April 2022 13:19 UTC](https://discourse.nodered.org/t/payload-to-base64/61447 "2022-04-18T13:19:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rex/32/59848_2.png) [@rex](https://discourse.nodered.org/u/rex)\
**Post date:** [18 April 2022 13:19 UTC](https://discourse.nodered.org/t/payload-to-base64/61447/1 "2022-04-18T13:19:56Z")

</div>

I'm trying to encode a simple string into base64 and it doesn't work. Where is my mistake?

This is my simple flow. It is an injection of payload string and I use msg.payload.toString('base64') function that return the payload unchanged. ☹

```auto
[
    {
        "id": "2b928680670e4e84",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "edf6c1fde29fbbc9",
        "type": "inject",
        "z": "2b928680670e4e84",
        "name": "Off",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0",
        "payloadType": "str",
        "x": 270,
        "y": 220,
        "wires": [
            [
                "c813605ba3679017"
            ]
        ]
    },
    {
        "id": "c813605ba3679017",
        "type": "function",
        "z": "2b928680670e4e84",
        "name": "",
        "func": "\nmsg.payload.toString(\"base64\");\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 480,
        "y": 220,
        "wires": [
            [
                "d042f113ad2a7a3a"
            ]
        ]
    },
    {
        "id": "d042f113ad2a7a3a",
        "type": "debug",
        "z": "2b928680670e4e84",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 670,
        "y": 220,
        "wires": []
    }
]

```

This the result:

 ![immagine](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/da8ff6f7248680451505194af65dc9ff38e4430a.png)

Thanks for the help.

---

<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:** [18 April 2022 13:26 UTC](https://discourse.nodered.org/t/payload-to-base64/61447/2 "2022-04-18T13:26:50Z")

</div>

A low code solution is to use the base64 node. Otherwise...

```auto
const buf = Buffer.from(msg.payload)
msg.payload = buf.toString("base64");
return msg;

```

---

<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:** [18 April 2022 13:47 UTC](https://discourse.nodered.org/t/payload-to-base64/61447/3 "2022-04-18T13:47:36Z")

</div>

There is also a $base64enode() function that can be used in the change node, using JSONata.  
e.g.

```auto
[{"id":"edf6c1fde29fbbc9","type":"inject","z":"2b928680670e4e84","name":"Off","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":270,"y":220,"wires":[["1329b982.bc40ee"]]},{"id":"1329b982.bc40ee","type":"change","z":"2b928680670e4e84","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$base64encode($$.payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":220,"wires":[["d042f113ad2a7a3a"]]},{"id":"d042f113ad2a7a3a","type":"debug","z":"2b928680670e4e84","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":220,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![rex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rex/32/59848_2.png) [@rex](https://discourse.nodered.org/u/rex)\
**Post date:** [18 April 2022 14:17 UTC](https://discourse.nodered.org/t/payload-to-base64/61447/4 "2022-04-18T14:17:49Z")

</div>

Thanks. My node.js knowledge is not very good. Just to understand, the function toString('base64') work only applied to buffer object, that right?

---

<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:** [18 April 2022 15:05 UTC](https://discourse.nodered.org/t/payload-to-base64/61447/5 "2022-04-18T15:05:27Z")

</div>

Yes.

The easy way to understand this is to look at the docs.

For example, the [`buffer.toString`](https://nodejs.org/api/buffer.html#buftostringencoding-start-end) function accepts an `encoding` parameter

But [`string.toString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toString) does not.

---

<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:** [17 June 2022 15:06 UTC](https://discourse.nodered.org/t/payload-to-base64/61447/6 "2022-06-17T15:06:08Z")

</div>

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