Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instance.defaults.headers.common['Authorization'] deffrence from axios.defaults? #341

Closed
coolblabla opened this issue Jun 6, 2016 · 4 comments

Comments

@coolblabla
Copy link

commented Jun 6, 2016

throws TypeError: Cannot read property 'common' of undefined(…) while using custom instanec.

Custom instance defaults

// Set config defaults when creating the instance
var instance = axios.create({
  baseURL: 'https://api.example.com'
});

// Alter defaults after instance has been created
instance.defaults.headers.common['Authorization'] = AUTH_TOKEN;
@nickuraltsev

This comment has been minimized.

Copy link
Member

commented Jun 7, 2016

Yes, I think there is a bug either in the code or the documentation.

The README.md suggests that the following should work:

// Set config defaults when creating the instance
var instance = axios.create({
  baseURL: 'https://api.example.com'
});

// Alter defaults after instance has been created
instance.defaults.headers.common['Authorization'] = AUTH_TOKEN;

But instance.defaults is just a copy of the config passed to axios.create (https://github.com/mzabriskie/axios/blob/master/lib/core/Axios.js#L17):

function Axios(defaultConfig) {
  this.defaults = utils.merge({}, defaultConfig);
  // ...
}

So the example above won't work as instance.defaults.headers is undefined.

@mzabriskie Should we fix the code, the docs, or both?

@coolblabla

This comment has been minimized.

Copy link
Author

commented Jun 8, 2016

+1 I'd like to merge defaultConfig to instance by default. var instance = axios.create(axios.defaults);

@damianprzygodzki

This comment has been minimized.

Copy link

commented Jun 21, 2016

+1 Yeah, this is bug. But i think it is just wrong docs. For me defaults are defaults. We should leave it alone for axios "default" instance. And headers for instance set as baseURL, as a property of instance object.

@mzabriskie

This comment has been minimized.

Copy link
Member

commented Jun 23, 2016

I think a good solution would be to use defaults instead of {} here https://github.com/mzabriskie/axios/blob/master/lib/core/Axios.js#L17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.