axios add authorization header bearer code example
Example 1: axios bearer token
{
headers: {
'Authorization': 'Bearer ' + validToken()
}
}
Example 2: axios send bearer token
const config = {
headers: { Authorization: `Bearer ${token}` }
};
Axios.post(
'http://localhost:8000/api/v1/get_token_payloads',
config
).then(console.log).catch(console.log);
Example 3: axios header authorization
const res = await axios.get('https://httpbin.org/get', {
headers: {
authorization: 'my secret token'
}
});
Example 4: autherization token in axios
const api = 'your api';
const token = JSON.parse(sessionStorage.getItem('data'));
const token = user.data.id;
axios.get(api , { headers: {"Authorization" : `Bearer ${token}`} })
.then(res => {
console.log(res.data);
.catch((error) => {
console.log(error)
});