Axios configuration
Axios is Promise based HTTP client for the browser and node.js. You can see more detail about the same on the Axios GitHub Repository. There is a present configuration file that you can use to modify the behaviour of Axios. The file can be located in src/config/axios/axiosBaseInstance
. Here is how the base instance looks like and can be modified the way you want.
import axios from 'axios';
// Create the Axios instance
const axiosBaseInstance = axios.create({
// .. where we make our configurations
baseURL: 'https://api.example.com/api/'
});
Apart from the existing instance you can make multiple instances of Axios in the same directory based on your requirement.
Using Axios for making an API request
Making an API request with Axios is simple. This is how you can make a post request with axios.
import axiosBaseInstance from "../../../config/axios/axiosBaseInstance";
const response = axiosBaseInstance.post( 'login',
{
email: action.email,
password: action.password
});
This will send a post request to https://api.example.com/api/login
since https://api.example.com/api/
has been included in our base instance and can be replaced with the url you want to make request to. More details on how to use Axios can be found here: Axios GitHub Repository