axios post with authorization header 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: how to send header in axios
const username = ''
const password = ''
const token = Buffer.from(`${username}:${password}`, 'utf8').toString('base64')
const url = 'https://...'
axios.post(url, {
headers: {
'Authorization': `Basic ${token}`
}
})
Example 4: 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 5: axios default headers authorization
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
Example 6: axios post headers authorization
axios.post("url","Data").then(res => {
console.log(res)
localStroge.setItem("access",accessToken)
localStroge.setItem("refresh",refreshToken)
})