CodeSource code example

How To Use Axios Header On Your Request


In this article, we will learn how to use Axios Header on your request.

There is a slight difference in using the Axios Header on GET method compared to others.

Let’s say we want to put an Authorization header on our request, we can pass the header object as the second argument of the request.

// set headers for GET method
axios.get('https://yourdomain.com/api', {
  headers: {
    Authorization: 'Bearer ' + token 
  }
})

For the POST, PUT and also DELETE method, we have to pass the headers object as the third argument because the second one will be used as a payload object. For example, this one is for the POST method:

// set headers for POST method
const payload = {}
axios.post('https://yourdomain.com/api', payload, {
  headers: {
    Authorization: 'Bearer ' + token 
  }
})

Share on social media

//