Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why use axios if there is a fetch #3899

Closed
BataevDaniil opened this issue Jul 19, 2021 · 4 comments
Closed

Why use axios if there is a fetch #3899

BataevDaniil opened this issue Jul 19, 2021 · 4 comments

Comments

@BataevDaniil
Copy link

BataevDaniil commented Jul 19, 2021

Describe the issue

I want to understand the advantages of using axios compared to fetch. I suggest that everyone write an example code that will show why you are using axios or an example code that you think will be difficult to repeat using fetch. And I will throw off a simple example code that will do the same thing but using fetch and the usual approaches of writing code.

I want to show with these examples that using fetch you can do everything the same as using axios, but for this you do not have to learn a new library, you just need to have basic programming skills. Or during the discussion, I will understand that axios is more optimal than fetch.

The problems you face when using axis, unlike fetch

  1. You need to study a third-party library that was invented by ordinary developers, and fetch is used immediately in the browser.
  2. This library may die sooner or later
  3. Newcomers to the web will most likely know about the native fetch than axios
  4. Axios has a very different interface than fetch, so knowing the native fetch library, you can't use axios and vice versa.
  5. Since axios has various pre-configs, it can cause confusion that you do not need to pass a header if you pass json and so on, the library will think of a lot for you, and if there are questions, then you have to look at how it is implemented.

Example Code

Expected behavior, if applicable

Environment

  • Axios Version [e.g. 0.18.0]
  • Adapter [e.g. XHR/HTTP]
  • Browser [e.g. Chrome,]
  • Browser Version [e.g. 22]
  • Node.js Version [e.g. 13.0.1]
  • OS: [e.g. iOS 12.1.0, OSX 10.13.4]
  • Additional Library Versions [e.g. React 16.7, React Native 0.58.0]

Additional context/Screenshots

@jaybekster
Copy link

jaybekster commented Jul 20, 2021

The team already told you that one of the benfits of using that is the same api for this library in browser and nodejs environment. Moreover it has good approaches to use different testing utils to mock and unmock an instance of axios which can be used for specific endpoint. And these are not all bullet points of its pros

@Paul6552
Copy link

You find a lot already on stackoverflow to this question :-)

Example here

@lpar
Copy link

lpar commented Dec 7, 2021

Went looking for the answer to the question, found the following benefits of Axios over fetch:

  • Handles CSRF protection
  • Handles JSON decode automatically
  • Handles form data serialization automatically
  • Allows tracking of progress for large transfers
  • Supports timeouts and cancellation
  • Supports request/response interception for middleware and debugging
  • Throws errors when requests fail

@jasonsaayman
Copy link
Member

Closing off topic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants