Axios

For a developer, having access to API’s in order to create amazing websites is a critical part of what we do. Usually pulling information from these treasure troves involved some kind of fetch() call to retrieve the information via JSON, and from there we parse the JSON for the information we need.

Example:

returns the follow…

This way has been tried and true, but what if there was a better way? Introduce Axios, a promise based HTTP client for the browser and node.js. What exactly is a promise? good question, anytime we are making a fetch request we are expecting something back in return in our asynchronous call, this is our promise. This may be a failure to to reach the designated API or a successful call which would return the data we are attempting to output.

Getting Started

Ok lets get started. Great thing about Axios is it comes with its own library, in react simply run:

and your good to go!

Once installed we can go ahead and import axios in our application to create a call…

Lets break this down. Firstly, we are setting our fetch to an async function, async and await make sure that we receive something back in return from out fetch, also allows us to avoid the .then in our typical fetch. Secondly, we are simply using axois syntax axios.get along with our HTTP request to API we are attempintg to ping. Lastly, we are simply console logging our outcome. Once you pull up your console, you will see more then just the data that was fetched.

This information is return due to Axios, which is of great value when you get to starting needing to send/recieve information using Axios.

This is a quick look at axios and how to get started.

Happy Coding!!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gabriel Castro

Full Stack, Software Engineer. Focus in Rails, JavaScript and React.