Posts Learn Components Snippets Categories Tags About
/

How to make an Axios GET request

Learn how to perform an axios GET request from your web application to call an external API with ease

Created on Aug 27, 2021

20 views

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!

Snippets by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.

Load comments for How to make an Axios GET request

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your project.

View Components

Other Code Snippets

More