How to Make a GET Request with the React Axios Library

Axios is a library that is used to help us make external http requests in a similar fashion as fetch and returns http promises. I will go over a few types of requests. First you need to install Axios into the project using the npm command.

npm install axios

or using yarn…

yarn add axios

Then we will import it into our project.

const axios = require('axios');// or for ES6import axios from 'axios';

Now that we have it installed and imported into the project we can get to work on making http requests.

GET request:

Example…

const URL = "http://localhost:3000/subjects"
axios.get(`${URL}`)
.then((res)=>{
console.log(res)
});

from here we can use setState() in place on the console.log() to set the state of your retrieved data.

To be continued with… POST, PATCH and DELETE requests.

--

--