Send token in header axios code example
Example 1: axios header authorization
const res = await axios.get('https://httpbin.org/get', {
headers: {
authorization: 'my secret token'
}
});
Example 2: add authorization header axios
const res = await axios.get('https://httpbin.org/get', {
headers: {
'Authorization': 'my secret token'
}
});
Example 3: send token in axios header
const username = ''
const password = ''
const token = Buffer.from(`${username}:${password}`, 'utf8').toString('base64')
const url = 'https://...'
const data = {
...
}
axios.post(url, data, {
headers: {
'Authorization': `Basic ${token}`
},
})
Example 4: axios get request with headers in token
axios.get('https://api.github.com/user', {
headers: {
'Authorization': `token ${access_token}`
}
})
.then((res) => {
console.log(res.data)
})
.catch((error) => {
console.error(error)
})
Example 5: get token from post axios react
const refreshToken = () => { let currUser = JSON.parse(localStorage.getItem("my_app_user")); let getUserFormData = new FormData(); getUserFormData.append("grant_type", "refresh_token"); getUserFormData.append("refresh_token", currUser.refresh_token); return new Promise((resolve, reject) => { my_app .post(`${URL}/token/url/`, getUserFormData, { headers: { Authorization: "Basic {secret_key}" } }) .then(async response => { resolve(response); }) .catch(error => { reject(error); }); });};