axios heder code example

Example 1: header in axios

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

Example 2: 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 3: axios instance

// lets you create custom pre-configured fetch api call!
const getUser = axios.create({
  baseURL: 'https://randomuser.me/api/', // we define url
  timeout: 1000, // (optional) set timeout
  headers: {'X-Custom-Header': 'foobar'} // pass headers
});

// use later like this
getUser().then(response => console.log(response))