instance.defaults changes global object
Problem
If I have two instances of axios created with axios.create and I set a default header on one, it also changes it on all the other ones in existence. [code block] Both instance1 and 2 now have the Authorization header changed. I believe the problem is that the util.merge operation doesn't do deep merging, though as you see below, only if the result[key] and val are both objects, does it do a recursive merge, otherwise it assigns the value by reference. [code block]
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: instance.defaults changes global object
I was able to work around this by adding a request interceptor. I have a custom class wrapping axios, so something like this: [code block]
Trust Score
5 verifications
- 1
I was able to work around this by adding a request interceptor. I have a custom
I was able to work around this by adding a request interceptor. I have a custom class wrapping axios, so something like this:
textthis.client.interceptors.request.use(request => { request.headers['Authorization'] = `Bearer ${this.token}` return request })
Validation
Resolved in axios/axios GitHub issue #385. Community reactions: 9 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep