axios token code example

Example 1: axios bearer token

{
    headers: {
      'Authorization': 'Bearer ' + validToken()
    }
 }

Example 2: autherization token in axios

const api = 'your api'; 
const token = JSON.parse(sessionStorage.getItem('data'));
const token = user.data.id; /*take only token and save in token variable*/
axios.get(api , { headers: {"Authorization" : `Bearer ${token}`} })
.then(res => {
console.log(res.data);
.catch((error) => {
  console.log(error)
});

Example 3: header in axios

axios.post('url', {"body":data}, {
    headers: {
    'Content-Type': 'application/json'
    }
  }
)

Example 4: axios.interceptors.response.use

// Add a response interceptor
HTTP.interceptors.response.use(function (response) {
 return response
}, function(error) {
 if (error.response.status === 401) {
  store.dispatch('logout')
  router.push('/login')
 }
 return Promise.reject(error.response.data)
})