axios: Default Authorization header no longer working in 0.19.0

Describe the bug Since axios 0.19.0, the default Authorization header is no longer set in query. It works in 0.18.X.

To Reproduce Create an axios adapter and set to it a default Authorization header, nothing is sent in the query.

axios.defaults.headers.common.Authorization = 'Bearer ANY SECURITY TOKEN';

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 6
  • Comments: 15 (2 by maintainers)

Most upvoted comments

axios global defaults (axios.defaults.headers) seem to no longer affect prevoiusly-created axios client instances. Is this an intended change?

Example: 0.18 - header is sent https://codesandbox.io/s/axios018-8fg9d 0.19 - header is not sent https://codesandbox.io/s/axios019-5ddn8

@glebtv this seems to in fact be intended. The documentation might need a bit of adjustment for this, I will have a look at it, however, it seems you may need to implement this:

instance.defaults.headers.common['Authorization'] = AUTH_TOKEN;

This will change the instance defaults rather than the global default. This makes sense, especially in this example.

@vserpokryl I think I made a mistake when I explained the problem.

I set default headers AFTER adapter creation…

I cannot know Bearer when application is loaded and token is refreshed regularly so it is mandatory to put it after

It works for me (Node.JS v10.14.2)

const axios = require('axios');
const { version } = require('axios/package');

console.log(`Axios version: ${version}`);

axios.defaults.headers.common['Authorization'] = 'Bearer test';

axios.get('https://api.ipify.org?format=json')
  .then(({ data, config }) => {
    console.log({
      data,
      headers: config.headers,
    });
  });

axios.get('http://api.ipify.org?format=json')
  .then(({ data, config }) => {
    console.log({
      data,
      headers: config.headers,
    });
  });

Result:

Axios version: 0.19.0

{ data: { ip: '91.201.71.191' },
  headers:
   { Accept: 'application/json, text/plain, */*',
     Authorization: 'Bearer test',
     'User-Agent': 'axios/0.19.0' } }

{ data: { ip: '91.201.71.191' },
  headers:
   { Accept: 'application/json, text/plain, */*',
     Authorization: 'Bearer test',
     'User-Agent': 'axios/0.19.0' } }

Same issue not working for me also after updating to 0.19.0

This seems to be intended - https://github.com/axios/axios/pull/1395