Axios not behaving as expected

Viewed 224

I am trying to create a clear/blank instance of axios to make requests with.

const instance = axios.create({
  baseURL: "https://someurl"
});

In other parts of my application I create have a base class that I use.

import axios from 'axios';

export default (baseUrl, apiKey) => {
  let api = axios.create();
  let accessToken = localStorage.getItem('access_token');

  api.defaults.timeout = 15000;
  api.defaults.headers.common['Accept'] = 'application/vnd.api+json';
  api.defaults.headers.post['content-type'] = 'application/vnd.api+json';
  api.defaults.headers.patch['content-type'] = 'application/vnd.api+json';
  api.defaults.headers.put['content-type'] = 'application/vnd.api+json';
  api.defaults.baseURL = baseUrl;
  api.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`;

  api.interceptors.request.use(config => {
    console.log(`${config.method.toUpperCase()} ${config.url}`);
    return config;
  });

  return api;
};

My question is why headers from my base class be pulling through to this new instance of axios I am creating? Is there a way to create a new instance of axios that does not include these other headers?

0 Answers
Related