Can't find variable: btoa
tonxxd opened this issue · comments
Describe the bug
Using Basic Auth with axios in an expo app causes this error. It was working before upgrading both axios and expo, not sure what is causing the issue but seems btoa function is not available in expo envirorment.
To Reproduce
Make a get request with auth headers from an expo app
let axiosInstance = axios.create({
baseURL: 'http://example.com',
auth: {
username: 'user',
password: 'password'
}
});
axiosIstance.get('/path/to');
Environment:
- Axios Version 0.18.0
- expo 32.0.0
- react 16.5
I'm experiencing this same bug in axios 0.19.0. Downgrading to 0.18.0 seems to help for now.
Well, seems like the btoa polyfill has been removed, which causes this issue: v0.18.0...v0.19.0
Also removed in v0.18.1
v0.18.0...v0.18.1
Hi, I'm new in GitHub, however let me try to help.
Solved the current issue (Axios Removed btoa variable Polyfill) with these steps: (Used in React-Native Project)
- Install base-64 npm package, npmjs.com/package/base-64 ,
- You can set atob and btoa as global variables on React Native. Then, you won't need to import them on each file you need it. Add the following at beginning of your index.js, so that it can be loaded before another file uses atob and btoa:
import {decode, encode} from 'base-64'
if (!global.btoa) {
global.btoa = encode;
}
if (!global.atob) {
global.atob = decode;
}
Hopefully Helps.
Thanks.
put "axios": "0.18.0" in project dependencies
that's worked for me
Adding following LoC in application tag of AndroidManifest.xml helped me:
android:usesCleartextTraffic="true"
What's the status with this? Run into this with axios v19 and expo v35.
What's the status with this? Run into this with axios v19 and expo v35.
Any solution?
@Alanscut How much time does it usually requires?
@Alanscut @SyneticDevOps According to #1689, it is not a bug, but an intentional change. If you want an out-of-box usage, you have to lock axios to ~0.18.0. Or you should add global btoa
polyfill by yourself.
@proIT324 the same for me !!
@Alanscut @SyneticDevOps According to #1689, it is not a bug, but an intentional change. If you want an out-of-box usage, you have to lock axios to ~0.18.0. Or you should add global
btoa
polyfill by yourself.
This issue still exist.
My app is in react-native and I ended up solving this with the following:
yarn add react-native-base64
in my login action:
import base64 from 'react-native-base64';
const authHeader = 'Basic ' + base64.encode(`${username}:${password}`);
const response = axios.post(LOGIN_ENDPOINT, {}, {
headers: { 'Authorization': authHeader }
});
it worked!!!
@chinesedfan What is your team's intention to remove btoa
from Axios?
same for me