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

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
Copy link
Member

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
Copy link
Author

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

@damianprzygodzki
Copy link

+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
Copy link
Member

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

@axios axios locked and limited conversation to collaborators May 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants