Overview

Axios is a Javascript library used to make HTTP requests similar to fetch(). It is supported by many different browsers including Chrome, Firefox and IE. However Axios comes with certain features that fetch does not have.

How to use

To install Axios to your project run the command

npm instal axios --save
import axios from 'axios'
axios.get('http://localhost/api').then(res => res.json())

Interceptors

Axios comes with something called interceptors which is useful when you want to examine or change HTTP requests.

axios.interceptors.request.use(config => {
console.log('Request sent');

return config;
});

// sent a GET request
axios.get('https://localhost/api')
.then(response => res.json());

Simultaneous Requests

Axios provides a way to make simultaneous requests using all() and spread().

axios.all([
axios.get('https://localhost/api/1'),
axios.get('https://localhost/api/2')
])
.then(axios.spread((res1, res2) => {
// Both requests are now complete
console.log(res1);
console.log(res2);
}));

Conclusion

Using Axios is similar to fetch but it comes with extra features including interceptors and making simultaneous requests. If you do not require the use of the extra features provided by Axios it might be better to stick with fetch instead. Axios is easy to use but there is also no reason not to stick with fetch.

Written by

Hi

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store