axios (Javascript Library) Cheatsheet

1059




Axios is promise based HTTP client for both clientside and server side

Installation

npm install axios

Example GET request

const axios = require('axios');

axios.get('https://httpbin.org/get')
        .then(function (response) {
            // handle success
            console.log(response);
        })
        .catch(function (error) {
            // handle error
            console.log(error);
        })
        .finally(function () {
            // always executed
        });

Example GET request (using async/ await)

const response = await axios.get('https://httpbin.org/get');
console.log(response);

Example POST request

axios.post('https://httpbin.org/post', {
            foo: "bar"
        })
        .then(function (response) {
            console.log(response);
        })
        .catch(function (error) {
            console.log(error);
        });