Axios vs. Got vs. Fetch: Which Should You Choose

June 21, 2024 ยท 10 min read

Are you seeking the perfect HTTP library for your next web scraping task? Axios, Got, and Fetch all offer intuitive solutions for making web requests in NodeJS. However, each has distinct approaches and features you must consider.

In this article, you'll explore the strengths and weaknesses of all these libraries, along with the main use cases for each. After reading, you'll be all set to pick the best toolkit for your project!

Got, Axios or Fetch API? Quick Answer

Got and Axios are third-party libraries providing higher-level abstractions, significantly reducing the amount of code required for a basic request. They share many similarities but have different built-in functionalities, such as HTTP/2 support and auto retries.

On the other hand, Fetch is a built-in API that exposes the underlying details of the HTTP protocol, allowing you to customize almost every aspect of your request. While this can be valuable in certain use cases, it requires a more verbose syntax.

In essence, the best HTTP library for you depends on your project needs and requirements. Consider the functionalities most important to you, for example:

  • Use Got if out-of-the-box HTTP/2 support and automatic retries are critical to your project.
  • Use Fetch if you want complete customization control and are comfortable handling request and response objects more manually.
  • Use Axios if you prioritize a simple syntax and ease of use.

For more clarity on when to use each tool, let's summarize their key features side by side. The comparison table below highlights the libraries' strengths and differences:

Criteria Axios Got Fetch API
Syntax Easy-to-use syntax Slightly complex syntax Verbose syntax
Performance Fast Fast Fast
Browser support Supports both modern and old browsers Designed primarily for NodeJS environments Supports only modern browsers (Chrome 42+, Firefox 39+, Edge 14+, and Safari 10.3+)
Features Feature-rich library, but lacks built-in support for HTTP/2 and retries Feature-rich library with built-in support for HTTP/2 and retries Lacks advanced features
Popularity The most popular NodeJS HTTP library Established but not as popular as Axios Very popular browser API
Customization Moderate level of customization with configurable options for common HTTP request features Provides extensive options for configuring HTTP requests, including headers, query parameters, timeouts, retries, redirects, caching, and more Highly customizable, providing full control for lower-level details
Community support The largest community of NodeJS HTTP library users Strong community with extensive support Large community with well-structured documentation

Now that you have an overview of each tool's capabilities, let's dive deeper into each feature.

Frustrated that your web scrapers are blocked once and again?
ZenRows API handles rotating proxies and headless browsers for you.
Try for FREE

Axios vs. Got vs. Fetch: Comparison

The table above shows Axios, Got, and Fetch have different strengths and weaknesses. Let's explore them in more detail.

Axios Is the Most Popular

While Got and Fetch are well known, Axios is the most popular HTTP library in NodeJS, proven by its 12.5+ million GitHub users and 210 million monthly npm downloads.

Axios has many active contributors, so it's regularly updated to address bugs and introduce new features. The extensive community support makes it even easier to use and troubleshoot, as you can quickly find resources on any issue.

Axios Has the Easiest Syntax

One of Axios' main selling points is its easy-to-use and readable syntax. A NodeJS beginner developer can quickly read and understand Axios code as it provides a high-level abstraction for most HTTP request tasks. For example, Axios offers methods like then() that make it easy to handle HTTP responses.

Although Got offers a similar high-level API, its syntax is slightly more complex, especially with advanced features. For example, Got nests its configuration options within the main requests, while Axios allows you to pass options directly as parameters to methods like axios.get().

Similarly, Fetch requires more familiarity with low-level HTTP concepts and verbose code. It's necessary for the implementation of advanced features that may already be abstracted with predefined methods in Axios and Got.

Most notably, Fetch often requires additional steps to handle various aspects of HTTP requests and responses. For example, unlike Axios, which automatically parses JSON responses, Fetch returns the raw response body by default. So, you need to explicitly parse response data, especially when dealing with JSON.

Got Wins on Performance

The performance of Axios, Got, and Fetch can depend on various factors, such as network conditions, server responsiveness, and specific use cases. Each library has unique performance characteristics.

Fetch is a native API that does not require external libraries or dependencies. This makes it a more efficient option than third-party libraries like Axios and Got.

However, if we compare libraries only, Got takes the crown. It's lightweight and has a performance-centric design, which means it has minimal overhead. This can lead to faster execution time and better memory usage compared to other libraries. Also, Got's built-in support for HTTP/2 support can further enhance its performance.

Axios focuses more on making it easy for users to make HTTP requests with its intuitive API, developer-friendly features, and clear syntax. This can influence performance by streamlining development workflows, reducing errors, and providing flexible default configurations like timeouts.

Let's do a quick performance test for the three libraries for more insights on the performance speed.

The code snippet below measures the time taken to make a GET request using Axios, Got, and Fetch.

sample.js
// import the required libraries
import axios from 'axios';
import got from 'got';
 
// define the target URL
const url = "https://httpbin.io/ip";
 
// got
console.time("Got");
(async () => {
    try {
        const response = await got(url);
        console.log(response.body);
        console.timeEnd("Got");
    } catch (error) {
        console.log(error.response.body);
    }
})();
 
// axios
console.time("Axios");
axios.get(url)
    .then(response => {
        console.log(response.data);
        console.timeEnd("Axios");
    })
    .catch(error => {
        console.log(error.response.data);
    });
 
// fetch
console.time('Fetch');
 
fetch(url)
  .then((response) => response.json())
  .then((data) => {
    console.log(data);
    console.timeEnd('Fetch');
  });

Here's the result:

Terminal
 0.06 KB | None |  
     
Fetch: 1.077s
//...
Got: 1.093s
//...
Axios: 1.095s
//...

The results are similar for the three libraries. Fetch is slightly faster than the others, recording 1.07 seconds. Axios and Got are close with 1.095 seconds and 1.093 seconds, respectively.

Got, Axios, and Fetch speed test
Click to open the image in full screen

A better benchmark with varied test cases could produce more distinct values, but the results above make it clear that the raw time taken for the request is not a significant differentiating factor for these libraries.

Axios and Fetch Support Browsers

Axios and Fetch offer browser support, whereas Got is primarily designed for NodeJS environments.

Axios works in both browser and NodeJS environments. It uses XMLHttpRequest (XHR) under the hood to make HTTP requests in browser environments. This makes Axios backward-compatible. In other words, it can run on old browsers like IE11 without any issues.

Fetch is originally a web API and can be used directly in browser-based JavaScript without an external library. However, it isn't backward compatible and only supports modern browsers (Chrome 42+, Firefox 39+, Edge 14+, and Safari 10.3+).

Got Has the Most Comprehensive Features

Both Got and Axios are feature-rich NodeJS libraries. They offer features such as automatic redirects, response caching, timeouts, and request cancellation.

However, Got provides a few advanced features that Axios doesn't. For example, Got has built-in support for automatic retries and HTTP/2. Also, its automatic decompression of compressed responses facilitates efficient data transfer and guarantees faster response times.

On the other hand, the Fetch API offers extensive customization options rather than out-of-the-box advanced features. This allows you to customize according to your project requirements.

Best Choice to Avoid Getting Blocked While Scraping

A NodeJS HTTP library with the right features and decent performance is only one part of success. You also need to find a way to avoid bans and blocks, which is one of the main challenges of at-scale web scraping. Websites implement increasingly sophisticated anti-scraping techniques to regulate bot traffic, so to stand a chance, you must mimic a real user's request.

One popular quick fix is using proxies to distribute traffic across multiple servers, making your request appear to originate from different users. Got, Fetch, and Axios allow you to easily route requests through proxies. However, this isn't an effective method against advanced anti-bot measures.

The best way to avoid getting blocked is to use complete web-scraping solutions, such as ZenRows. This web scraping API offers the complete toolkit necessary to scrape any website without getting blocked. By automatically rotating premium proxies, optimizing headers, implementing anti-CAPTCHAs, and more, ZenRows enables you to bypass any anti-bot system. Plus, it integrates seamlessly with Got, Axios, and Fetch.

Conclusion

Got, Axios, and Fetch are all excellent choices for making HTTP requests in NodeJS. However, the best choice for you depends on your specific requirements:

  • For use cases requiring extensive customization and optimal performance, Fetch is the best fit
  • If ease of use is a critical factor, go for Axios.
  • Use Got if you need a feature-rich library with built-in HTTP/2 support.

That said, many websites will still block your scraper regardless of the HTTP client you use. Integrate ZenRows with your web scraper today, and forget about getting blocked. Try ZenRows for free!

Ready to get started?

Up to 1,000 URLs for free are waiting for you