Axios API Calls

Mock API calls

Set default axios baseURL for call API

Open .env file and edit VITE_APP_API_URL.

.env
## Backend API URL
VITE_APP_API_URL=

Axios has been configured in the folder src\utils\authAxios.js

Example 1: With baseUrl

src\utils\authAxios.js
// axios
import axios from 'axios';
const axiosServices = axios.create({ baseURL: import.meta.env.VITE_APP_API_URL || 'http://localhost:3010/' });

// ==============================|| AXIOS SERVICES ||============================== //

axiosServices.interceptors.response.use(
  (response) => response,
  (error) => {
    if (error.response.status === 401 && !window.location.href.includes('/login')) {
      window.location.pathname = '/login';
    }
    return Promise.reject((error.response && error.response.data) || 'Wrong Services');
  }
);

export default axiosServices;

Example 2: Without baseUrl

You can set the entire URL in Axios request. Do not use common Axios instances src\utils\axios.jsinstead use directly Axios library.

src\utils\axios.js
// axios
import axios from 'axios';
const axiosServices = axios.create();

// ==============================|| AXIOS SERVICES ||============================== //

axiosServices.interceptors.response.use(
  (response) => response,
  (error) => Promise.reject((error.response && error.response.data) || 'Wrong Services')
);

export default axiosServices;

Last updated