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:

Rxjs Adoption Guide: Overview, Examples, And Alternatives

RxJS adoption guide: Overview, examples, and alternatives

Get to know RxJS features, benefits, and more to help you understand what it is, how it works, and why you should use it.

Emmanuel Odioko
Jul 26, 2024 ⋅ 13 min read
Decoupling Monoliths Into Microservices With Feature Flags

Decoupling monoliths into microservices with feature flags

Explore how to effectively break down a monolithic application into microservices using feature flags and Flagsmith.

Kayode Adeniyi
Jul 25, 2024 ⋅ 10 min read
Lots of multi-colored blue and purplish rectangles.

Animating dialog and popover elements with CSS @starting-style

Native dialog and popover elements have their own well-defined roles in modern-day frontend web development. Dialog elements are known to […]

Rahul Chhodde
Jul 24, 2024 ⋅ 10 min read
Using Llama Index To Add Personal Data To Large Language Models

Using LlamaIndex to add personal data to LLMs

LlamaIndex provides tools for ingesting, processing, and implementing complex query workflows that combine data access with LLM prompting.

Ukeje Goodness
Jul 23, 2024 ⋅ 5 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