FG
๐Ÿ’ป Software๐Ÿ”Œ APIs & SDKs

instance.defaults changes global object

Freshalmost 8 years ago
Mar 14, 20260 views
Confidence Score81%
81%

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

Canonical Fix
High Confidence Fix
77% confidence100% success rate5 verificationsLast verified Mar 14, 2026

Solution: instance.defaults changes global object

Low Risk

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]

77

Trust Score

5 verifications

100% success
  1. 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:

    text
    this.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

Worked: 5
Partial: 1
Last verified Mar 14, 2026

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

axioshttpapi