1

I am having a config file . where i am setting the baseURL for the entire app and also saving the bearer token for the entire API requests. Here i am in situation to add another api . I dont know how to add another baseURL & use this on my API requests.Here i am sharing the code of what i have done.

BASE URL FILE:

import axios from 'axios';
axios.defaults.baseURL = http://localhost:3000/summary;

const setAuthToken = (token) => {
  if (token) {
    axios.defaults.headers.common.Authorization = `Bearer ${token}`;
  } else {
    delete axios.defaults.headers.common.Authorization;
  }
};

export default setAuthToken;

API ACTION FILE:

export const login = ({ email, password }) => async (dispatch) => {

  const userData = {
    username: email,
    password,
  };

  try {
    const res = await axios.post('/license-api/auth/login', userData, config);
    dispatch({
      type: LOGIN_SUCCESS,
      payload: res.data.token,
    });
  } catch (error) {
    dispatch({
      type: LOGIN_FAIL,
    });

  }
};

i need to add another url like this in BASE URL FILE

axios.defaults.baseURL = http://localhost:6000/profile

how to add this one and use this in API action file.

Please help me with this.

Thanks in advance

3
  • Create two different client instances with different values? npmjs.com/package/axios#creating-an-instance Commented Mar 26, 2020 at 8:13
  • HI , how to use that instance in the action fie? Commented Mar 26, 2020 at 8:15
  • Import it from where it's defined, as with anything else. Commented Mar 26, 2020 at 8:17

2 Answers 2

5

As said you could create two instances of axios and use them as needed:

In you BASE URL file:

import axios from 'axios';

const setAuthToken = (token) => {
  if (token) {
    axios.defaults.headers.common.Authorization = `Bearer ${token}`;
  } else {
    delete axios.defaults.headers.common.Authorization;
  }
};

const mainAxios = axios.create({
  baseURL: 'http://localhost:3000/summary'
});

const profileAxios = axios.create({
  baseURL: 'http://localhost:6000/profile'
});

export default setAuthToken;
export { mainAxios, profileAxios };

Then in your API ACTION file:

import { profileAxios } from 'path/to/baseurl';

export const login = ({ email, password }) => async (dispatch) => {

  const userData = {
    username: email,
    password,
  };

  try {
    const res = await profileAxios.post('/license-api/auth/login', userData, config);
    dispatch({
      type: LOGIN_SUCCESS,
      payload: res.data.token,
    });
  } catch (error) {
    dispatch({
      type: LOGIN_FAIL,
    });

  }
};
Sign up to request clarification or add additional context in comments.

Comments

0

The above code works well. I don't see where setAuthToken is called so if you don't want to call setAuthToken manually then you might want to do this.

import axios from "axios";
import { config } from "./config";

const pythonAPI = axios.create({
  baseURL: config.pythonServerUrl,
});

const nodeApi = axios.create({
  baseURL: config.nodeServerUrl,
});

const setToken = () => {
  const token = localStorage.getItem("auth_token");
  if (token) {
    pythonAPI.defaults.headers.common.Authorization = `Basic ${token}`;
  } else {
    delete pythonAPI.defaults.headers.common.Authorization;
  }
};

const pythonApi = {};

pythonApi.get = async (url) => {
  setToken();
  return pythonAPI.get(url).catch((e) => e.response);
};

pythonApi.post = async (url, data) => {
  setToken();
  return pythonAPI.post(url, data).catch((e) => e.response);
};

export { pythonApi, nodeApi };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.