Axios is caching somehow my get user request react native
The issue appears to be that the request is being sent before the header has actually been set. The reason for that is because setHeader
internally relies on a callback to fire before actually setting the header, and theres no hook to allow the calling code to wait for this code to finish before firing off the request.
It's fixable though, make setHeader
return a Promise
and resolve with the config
object
export const setHeaders = (type, props) => {
return new Promise(resolve => {
AxiosInstance.interceptors.request.use(config => {
config.headers[type] = {};
props.forEach((prop) => {
config.headers[type][prop.key] = prop.value;
});
return resolve(config);
});
});
}
Then in getUserByToken
, await
the header
getUserByToken: async function (token) {
var headers = [
{ key: 'Authorization', value: 'Bearer ' + token},
{ key: 'Content-Type', value: 'application/x-www-form-urlencoded'},
{ key: 'Cache-Control', value: 'no-cache'}
];
await setHeaders('get', headers);
return AxiosInstance.get('/user');
}