DEV Community 👩‍💻👨‍💻

José Pablo Ramírez Vargas
José Pablo Ramírez Vargas

Posted on • Updated on

Open Discussion: Why People Prefer Axios over Fetch?

Hello community! I come with what I consider an interesting question because I continue to see the Axios library referred a lot everywhere, even in simple samples, as opposed to the standardized fetch() function which is now also available in NodeJS v18.

My question for you all is: Why do you (or others, in your opinion) continue to prefer Axios over fetch()? I am no front-end developer, so pardon my ignorance if it is a big thing I am unaware of. Yes, I do my backend mostly in ASP.net.

I'll try to compile all your answers here by editing regularly as people respond.

Thank you!


Results

Ok, so let's start tabulating things:

Feature Fetch Axios
Master configuration (headers and such) ✔️
People used to Axios ✔️
No extra library (still needs wrapper) ✔️
No installation ✔️
XSRF ✔️
Download progress ✔️ ✔️
Upload progress ✔️
1-step JSON response ✔️

Top comments (13)

Collapse
 
naveennamani profile image
naveennamani

If your app is a single page app and heavily interacts with backend through APIs you may notice that most of the methods require common user data to pass to backend. For example a token header or custom cookies etc. If you use fetch then for every possible endpoint you've to write all those things for each fetch request. However, in axios you can create a session with commonly required data that needs to be sent, and in your actual call to endpoint you only add additional information that is required.

I've not used axios more than this, so I would also like to know more points from others.

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

Awesome. Thanks for sharing. Indeed, my team is using fetch() but we do have the requirement you say. Makes me wonder how they did it with fetch(). Maybe I should check their code. I fear I'll find a mess! Hehe.

So this sounds like a solid point in favor of Axios.

Collapse
 
marklai1998 profile image
Mark Lai

FYI, there are apiClient out there uses fetch, I personally use Wretch daily

dev.to/marklai1998/why-im-ditching...

Collapse
 
shinigami92 profile image
Shinigami
  • NestJS uses axios for HttpService
  • it’s "easy" to use interceptors to add e.g. meta data to all requests/responses (like traceId and timer to log how long a call needed)
  • By default json is assumed / you don’t need res.json() everywhere
Collapse
 
jfbrennan profile image
Jordan Brennan • Edited on

Fetch still needs a little wrapper IMO, but I’m more than willing to maintain those 20-30 loc than import a dependency that I’ll only use 20% of. That’s why I switched to fetch.

Collapse
 
junepark202012 profile image
junepark202012 • Edited on

Also, check this geeksforgeeks link

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

Thanks for stopping by. some interesting points in that table, although one is clearly wrong, as fetch() can be cancelled through an AbortController object. Also the first one in the table sounds suspicious because fetch()'s Request object does have the url property.

In any case, I'll tabulate some more. Cheers!

Collapse
 
junepark202012 profile image
junepark202012

Didn't know, thanks for sharing!

Collapse
 
jackmellis profile image
Jack

I haven't used axios in 5+ years now. A small wrapper around fetch is all you really need.

Collapse
 
marklai1998 profile image
Mark Lai

FYI, there are apiClient out there uses fetch, I personally use Wretch daily

dev.to/marklai1998/why-im-ditching...

Collapse
 
nexxeln profile image
Shoubhit Dash

In my opinion fetch has gotten really good over the years and there's no real use case for axios. I think people just use it because they are used to it.

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

Alright. Fair enough. Tabulated the reason. Many thanks!

Collapse
 
ninhnd profile image
Dang Ninh

Mainly about master configuration. It's all about the DRY principle all over again.

DEV

Thank you.

 
Thanks for visiting DEV, we’ve worked really hard to cultivate this great community and would love to have you join us. If you’d like to create an account, you can sign up here.