2024-01-29
2513
Faraz Kelhini
5917
Jan 29, 2024 ⋅ 8 min read

Axios vs. fetch(): Which is best for making HTTP requests?

Faraz Kelhini JavaScript developer.

Recent posts:

Integrating Django Templates With React For Dynamic Webpages

Integrating Django templates with React for dynamic webpages

Create a dynamic demo blog site using Django and React to demonstrate Django’s server-side functionalities and React’s interactive UI.

Kayode Adeniyi
Apr 18, 2024 ⋅ 7 min read
Using Aoi Js To Build A Bot For Discord

Using aoi.js to build a bot on Discord

Explore how the aoi.js library makes it easy to create Discord bots with useful functionalities for frontend applications.

Rahul Padalkar
Apr 17, 2024 ⋅ 9 min read
Web Components Adoption Guide: Overview, Examples, And Alternatives

Web Components adoption guide: Overview, examples, and alternatives

Evaluate Web Components, a set of standards that allow you to create custom HTML tags for more reusable, manageable code.

Elijah Asaolu
Apr 16, 2024 ⋅ 11 min read
Using Aws Lambda And Aws Cloudfront To Optimize Image Handling

Using AWS Lambda and CloudFront to optimize image handling

Leverage services like AWS Lambda, CloudFront, and S3 to handle images more effectively, optimizing performance and providing a better UX.

Nitish Sharma
Apr 12, 2024 ⋅ 12 min read
View all posts

20 Replies to "Axios vs. <code>fetch()</code>: Which is best for making HTTP requests?"

  1. i’m not a developer or “power” user when it comes to my system – i’m wondering if axios is for someone like me. i have to upload files to my vendor’s ftp site – i can’t use google drive or dropbox because the vendor wants the files placed inside their ftp space. i can’t use fetch because it’s no longer supported by my OS, mojave. 🙁

    TIA! Dannielle

  2. There is also the fact that axios handles error responses differently from fetch.
    For fetch only network errors are actual errors.
    For axios perfectly successful server communication that happens to return 400+ responses is also an error.

  3. thanks for the amazing explanations and demonstrations.
    you made the hello world more fetch-able
    i will use fetch more thanks2u
    Shabat Shalom => (-_0)

    1. let fileSize = ”; // you can get fileSize in input[type=file] onchange event
      let uploadedByte = 0;
      fetch().then(res => {
      let reader = res.body.getReader();
      reader.read().then(({ done, value }) => {
      if (done) {
      console.log(‘upload completed’);
      }
      uploadedByte += value.byteLength;
      console.log(‘uploaded: ‘ + uploadedByte);
      console.log(‘progress: (uploadedByte/fileSize * 100).toFixed());
      });
      })

  4. Nice article and a great source of info when you are trying to implement all of these features.

    I would also include that fetch is stricter than XHR when it comes to CORS requests and cookies.
    Specifically, fetch does not send cookies on CORS requests, unless { credentials: ‘include’ } is used and once you do that, the Access-Control-Allow-Origin header can no longer be “*”.

    This may be a good thing or a bad thing depending on your use case I suppose. In my previous company, we had a corporate proxy that used cookies and it completely broke all of our CORS requests to public APIs that only send back Access-Control-Allow-Origin: “*” instead of parroting our Origin header. We had to actually force the polyfill on all browsers in order to fallback to XHRs and avoid the issue all together.

    See details here: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

  5. I need to do that for work and have used Cyberduck. I believe it is still free, and works like a champ on Mac.

  6. I recently needed to log the results of multiple API calls, and the log needed to contain both the response status and a small extract of the response body.

    fetch() made this quite difficult to do both at once whilst also keeping the code clean, since getting the response body is another level of async (for some reason?).

    It looks like this would have been trivial to do in Axios e.g:

    const logCallInformation = response => {

    const responseStatus = response.status
    const usefulData = extractUsefulBit(response.data)
    logger.log(options, responseStatus, usefulData)
    }

    axios(options).then(logCallInformation)

    So +1 for Axios from me.

    1. fetch makes much more sense here, since it allows you to not process a response after looking at the headers, while doing what you want isn’t difficult at all either:

      const response = await fetch(options)
      const data = await response.json()
      logger.log(options, response.status, extractUsefulBit(data))

  7. fetch() not always working properly. I tried to post request to express.js server – but i haven`t had success. The fetch return underined result (in .then(data=>{})) afer result.json()…

  8. I love your articles, but this is an unfair comparison. The only fair comparison for fetch is XHR. It would be more helpful and fair if you compared axios with fetch wrappers like ky, ofetch and wretch. I hope you’ll consider doing such a comparison as I’m having trouble deciding on a wrapper!

Leave a Reply