How to send the authorization header using Axios

  • Post author:
  • Post category:Bugs / React Js
  • Post last modified:July 7, 2021
  • Reading time:1 mins read

Axios is mostly used javascript promise-based HTTP client for end-to-end requesting resources from the serve side application. Generally, in simple application we don’t have to put the header for any get or post request. but most of the cases, security is a big issue sometimes we need to add authorization header to get resources from the server or save resource to the server. That’s why we need to add the headers with request.

So, let’s create a header object according you needs:

 const token = "your_token"

let headers = {
        "Content-type": "application/json; charset=UTF-8",
        "Authorization": 'Bearer ' + token
 };

token is a variable which store the authorization token is used for accessing the resources.

After that the request will be like:

For Get Request:

 axios.get('/get/projects', {
            headers: headers
})

For Post Request: (without header)

axios.post(url, {
  data: {
    ...
  }
})

For Post Request: (with header)

axios.post(url, {
  //...data
}, {
  headers: {
    ...
  }
})

Hope it Helps!