Fetch v/s Axios — Which is better for making http requests?

Sam
4 min readMay 30, 2021
tempIMG

While working with JavaScipt or any Web Application one has to make HTTP requests to servers pretty much all the time to get data, but developers are often time confused between the using the fetch API or the Axios library, they are both very similar in functionality and fetch is capable of doing all that Axios can do but there is an extra step, so in this blog we shall briefly explain each one so by the end you will be able to make the right decision for your use-case so let’s go.

Overview

Fetch: It is an API in JavaScript which provides fetch() method defined on the window object. It also provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline (requests and responses). It has one compulsory argument and that is the URL of the resource to be fetched and it returns a Promise that can be used to retrieve the response of the object.

Syntax — fetch

fetch_syntax1
Syntax of fetch API

Response — fetch

Fetch_response1
Response using fetch API

Axios: It is a JavaScript library used to make HTTP requests from node.js or XMLHttpRequests from the browser and it supports the Promise API that is native to JS-ES6. It comes with the ability to cancel request and can be used to intercept HTTP requests and responses, it also enables client-side protection against XSRF which it’s competitor lacks.

Syntax — Axios

syntax_axios
Syntax of Axios library

Response — Axios

response_axios
Response using Axios library

It is clearly visible that the response using Axios is different than what we got from the fetch API because in fetch API there is an extra step that we need to do in order to convert the response received into JSON format which goes like.

Syntax — fetch

f_syntax_2
Extra step in the syntax of fetch API compared to Axios

Response — fetch

fetch_res_2

Now, we can observe that the responses are somewhat similar of both fetch and Axios. So, this is the extra step that we talked about earlier that one has to do in fetch API as compared to Axios Library.

Differences Between Axios library and fetch API.

Although both of these can be used to yield similar results and are similar in more than one ways, they have their fair share of differences which might make some people choose one over another. Such as listed below

Axios is a third party package which needs to be installed whereas fetch is built into most modern web browsers so no installation is required.

Axios has built in XSRF protection whereas fetch doesn’t.

Axios has wide browser support and this is a BIG ONE, fetch completely lacks support in Internet Explorer and this reason alone is a deal breaker for many.

Axios allows cancelling request and request timeout whereas fetch does not.

Axios performs automatic transforms of JSON data, whereas this is a two step process in fetch API .json() method is called on the response every time.

Axios has the ability to intercept HTTP requests.Fetch, by default, doesn’t provide a way to intercept requests.

This was a detailed comparison of fetch API and Axios Library, how to use them and hopefully after reading this blog you will be able to make the right decision as per your needs. PEACE.

--

--