I am trying to post two inputs with axios and I want to base64 encode them before I send them.
To base64 encode string you can pipe an echo command into the base64 command-line tool. To ensure no extra, hidden characters are added use the -n flag. Without the -n flag you may capture a hidden characters, like line returns or spaces, which will corrupt your base64 encoding.
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
Deprecated since v6
const encodedString = new Buffer('your string here').toString('base64');
Use Instead
const encodedString = Buffer.from('your string here').toString('base64');
Is better you use Buffer.from('Your String').toString('base64'), because new Buffer is deprecated
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With