I'm using Axios ("axios": "^0.19.0")
to do a GET request to my backend which works as I already tested it by sending the same token through Postman macOS app, and returns the correct user object.
But from my React Native app, whenever I do the get request and pass the same bearer token, I get the last user I logged in as a response.
This is how I'm sending the request:
getUserByToken: function(token) {
var headers = [
{ key: 'Authorization', value: 'Bearer ' + token},
{ key: 'Content-Type', value: 'application/x-www-form-urlencoded'},
{ key: 'Cache-Control', value: 'no-cache'}
];
setHeaders('get', headers);
return AxiosInstance.get('/user');
},
The setHeaders method is used to set the headers to the request, I reset all http methods to an empty object and set the proper key-values for the following request.
export const setHeaders = (type, props) => {
AxiosInstance.interceptors.request.use(config => {
config.headers[type] = {};
props.forEach((prop) => {
config.headers[type][prop.key] = prop.value;
});
return config;
});
}
As you can see, I even attempted to use Cache-Control: no-cache but still, keeps caching for no reason.
This is how I'm calling this function from my AuthView.js
UserServices.getUserByToken(loginData.access_token)
.then(userResponse => {
// here userResponse is the previous user!
// even though i'm passing the correct token
this.props.onSetUserInfo(userResponse.data);
this.setState({
loading: false
}, () => {
startMainTabs();
});
});
Why could this be happening?
Thanks.
You can allow axios-cache-adapter to cache the results of a request using (almost) any HTTP method by modifying the exclude.
Axios is a widely used HTTP client for making REST API calls. You can use this in React Native to get data from any REST API. Features of Axios: It can make both XMLHttpRequests and HTTP requests.
Import axios and make a GET request Use npm i axios to install axios and then add it as an import to your parent component. I'm going to use axios to get all of my notes, which I stored in a database. Now that axios is installed, we can use it to make a GET request to retrieve all of the notes in our database.
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');
}
Please check with below code i have already tested same on my project and it's working fine.
getUserByToken: function(token) {
var headers = [
{ key: 'Authorization', value: 'Bearer ' + token},
{ key: 'Content-Type', value: 'application/x-www-form-urlencoded'},
{ key: 'Cache-Control', value: 'no-store'}
];
setHeaders('get', headers);
return AxiosInstance.get('/user');
},
Use same above code it will solved your problem.
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