send headers with axios code example
Example 1: axios header authorization
const res = await axios.get('https://httpbin.org/get', {
headers: {
authorization: 'my secret token'
}
});
Example 2: pass header in axios
const headers = {
'Content-Type': 'application/json',
'Authorization': 'JWT fefege...'
}
axios.post(Helper.getUserAPI(), data, {
headers: headers
})
.then((response) => {
dispatch({
type: FOUND_USER,
data: response.data[0]
})
})
.catch((error) => {
dispatch({
type: ERROR_FINDING_USER
})
})
Example 3: axios set authorization header
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: making axios call with headers
axios.post('url', {"body":data}, {
headers: {
'Content-Type': 'application/json'
}
}
)