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:

Building A Background Remover With Vue And Transformers.js

Building a background remover with Vue and Transformers.js

Build a real-time image background remover in Vue using Transformers.js and WebGPU for client-side processing with privacy and efficiency.

Emmanuel John
Nov 29, 2024 ⋅ 9 min read
Managing Search Parameters In Next.js With Nuqs

Managing search parameters in Next.js with nuqs

Optimize search parameter handling in React and Next.js with nuqs for SEO-friendly, shareable URLs and a better user experience.

Jude Miracle
Nov 27, 2024 ⋅ 10 min read
React logo over a beige background

Data fetching with Remix’s loader function

Learn how Remix enhances SSR performance, simplifies data fetching, and improves SEO compared to client-heavy React apps.

Abhinav Anshul
Nov 26, 2024 ⋅ 5 min read
Top 8 Fullstory Competitors And Alternatives

Top 8 Fullstory competitors and alternatives

Explore Fullstory competitors, like LogRocket, to find the best product analytics tool for your digital experience.

Jeff Wharton
Nov 26, 2024 ⋅ 13 min read
View all posts

22 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