Axios: Overridden headers on multiple instances creation

Created on 20 Jul 2017  ·  3Comments  ·  Source: axios/axios

Summary

I've noticed that when multiple instances of axios get created with custom headers configuration, the latest one will override any other previously defined configuration. Here is the simplest code snippet to reproduce it :

const axios = require('axios');
const baseURL = '';

const first = axios.create({ baseURL });
first.defaults.headers.common.authorization = 'foo';
const second = axios.create({ baseURL });
second.defaults.headers.common.authorization = 'bar';

console.log(first.defaults.headers.common);

Results in :

Object
    Accept: "application/json, text/plain, */*"
    authorization: "bar"
    ...

I am not sure this is the correct behavior since instances are supposed to be independant.

Context

  • axios version: e.g.: v0.16.2
  • Environment: e.g.: node v6.9.2

Most helpful comment

This is part of a greater issue that we're going to address soon (and way later than it's reasonable). See #812.

All 3 comments

I wish i could write first.headers.authorization = 'foo' and have it change for that instance

Well the only way to bypass this limitation seems to be forcing the header value :

first.get('/posts', {
    headers: {
        authorization: 'foo',
    },
})

This is part of a greater issue that we're going to address soon (and way later than it's reasonable). See #812.

Was this page helpful?
0 / 5 - 0 ratings