'How to base64 encode inputs in React?

I am trying to post two inputs with axios and I want to base64 encode them before I send them.



Solution 1:[1]

Deprecated since v6

const encodedString = new Buffer('your string here').toString('base64');

Use Instead

const encodedString = Buffer.from('your string here').toString('base64');

Solution 2:[2]

Consider using base-64 as well which is compatible with btoa and atob, worked for me in react and react native:

npm install base-64 --save
import {decode as base64_decode, encode as base64_encode} from 'base-64';
let encoded = base64_encode('YOUR_DECODED_STRING');
let decoded = base64_decode('YOUR_ENCODED_STRING');

In case you are using typescript, use @types/base-64 for typing

npm i --save-dev @types/base-64

Solution 3:[3]

Is better you use Buffer.from('Your String').toString('base64'), because new Buffer is deprecated

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Renato Mendes