Reputation: 659
I am trying to add token in my axios header using Asyncstorage and it gives the token as well but when I pass the token it shows me the error of 401.
const token = await AsyncStorage.getItem('token')
const response = await getRequest(`/login_api`, token);
const res = await axios.get(url, {
headers: {
'Authtoken': token
}
});
Copying and pasting the same token in the headers works just fine.
Upvotes: 2
Views: 415
Reputation: 659
I did everything but nothing worked then went through some documentation and found out using this in my axios client but still the token was not correctly saved so used JSON.parse as suggested by carlosdafield :
axiosClient.interceptors.request.use(
async config => {
const token = await AsyncStorage.getItem('token');
if (token) {
console.log(config)
config.headers.Authtoken = JSON.parse(token);
}
return config;
},
error => Promise.reject(error)
);
Upvotes: 1