Quick & Easy HTTP Requests Using Axios

January 03, 2019

Run:

npm install --save axios

GET Request:

const axios = require('axios');

// Make a request for a user with a given ID
axios.get('https://jsonplaceholder.typicode.com/posts')
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
  });

POST Request:

const axios = require('axios');

axios.post('https://jsonplaceholder.typicode.com/posts', {
    title: 'foo',
    body: 'bar',
    userId: 1
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

More Examples on the Axios repo