DEV Community

Open Discussion: Why People Prefer Axios over Fetch?

José Pablo Ramírez Vargas on October 16, 2022

Hello community! I come with what I consider an interesting question because I continue to see the Axios library referred a lot everywhere, even i...
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
 
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
 
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
 
junepark202012 profile image
junepark202012 • Edited

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
 
ninhnd profile image
Dang Ninh

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