Home / Snippets / How to make an Axios GET request
How to make an Axios GET request cover

How to make an Axios GET request

156

3 years ago

0 comments

What is Axios?


Axios is a promise-based HTTP client for browser applications and the node.js backend. It's a simple and easy-to-use library that many front-end applications depend on to call external API.

How to Install Axios


To get axios into your project you can use npm or yarn package manager.
npm install axios
yarn add axios

How to Perform GET request in Axios


To perform a GET request you can make use of the "get" method and passing in a URL that you want to request. Axios is also a promised-based library that also supports async/await request.
<script>
   import axios from 'axios';

   const response = await axios.get('https://jsonplaceholder.typicode.com/posts/1');

   console.log(response);
</script>
To perform request using the "then" you can write it as follows.
<script>
    import axios from 'axios';

    axios.get('https://jsonplaceholder.typicode.com/posts/1')
        .then(function (response) {
            console.log(response);
        });
</script>
Do note that you can also pass additional "parameters" like below.
<script>
    import axios from 'axios';

    axios.get('https://jsonplaceholder.typicode.com/posts/1', {
            params: { ID: 2 }
        })
        .then(function (response) {
            console.log(response);
        });
</script>
By now you should know How to make an Axios GET request, If you find it helpful do share it with your friends, and happy coding!
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this