GET request using axios with React Hooks

Today we’ll show you how to integrate GET request using axios with React Hooks.

We had created an article to call an API using the window.fetch method.

Axios is the promise based client for frontend and backend applications. Axios offers may more features compare to fetch.

In this article, we will create the following example using the axios package.

Demo Application

Output - GET request using axios with React Hooks - Clue Mediator
Output – GET request using axios with React Hooks – Clue Mediator

Table of Contents: GET request using axios

  1. Installing axios
  2. Syntax of the GET request using axios
  3. GET request with HTTP headers
  4. Example

1. Installing axios

Run the following command to install the axios with npm or yarn CLI.

npm CLI: npm install axios

yarn CLI: yarn install axios

2. Syntax of the GET request using axios

The following syntax will be used to call GET API using axios.

In the above code, we have used the get method of the axios and pass the URL in the first parameter.

3. GET request with HTTP headers

We can use the second parameter to pass the HTTP headers. Look at the following code where we are passing the Authorization and Custom-Header along with the same request.

4. Example

Here, we will use the bootstrap package to design the page where we will get the list of the data on page load.

App.js

Run the above file and check the output in the browser.

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

Demo & Source Code

GitHub Repository StackBlitz Project

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *