fetch vs axios — API consuming
While we want to request across the network, there are few API consuming libraries available on open-source platform.
- fetch
- axios
Fetch
Basically, it provides the object of Request and Response and other objects asynchronously across the network. It returns the Promise for the Request we make with the Response object.
Example:
You may pass headers to the request using the method and can add, append, set, delete the headers in the object.
Issues with error handling in fetch are only goes to the catch block in case of network error, else always will go to the success block and developers must manage the error handling by their own.
Using fetch, You need to use .json() method to get the data from the response. Hence, it takes two steps to retrieve the data from a response.
Fetch doesn’t provide any feature to cancel the request, any time-out or upload progress.
For Official Documentation : Fetch API
Axios
A JavaScript library to make promise-based http requests from the browser and node-js. It provides automatic transformation of json data which isn’t provided in fetch.
It requires installation from the open-source community.
For Documentation : axios
npm install –save axios
Example:
Few major differences for both the API consuming parties are as follow:
Clap !!
If you find this useful in any way.
Suggestions are always welcome.
Happy Coding !!