Payload to base64

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. :frowning:

[
    {
        "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:

Thanks for the help.

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

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

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

[{"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":[]}]
1 Like

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

Yes.

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

For example, the buffer.toString function accepts an encoding parameter

But string.toString does not.

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