September 5, 2023

How to Use a Proxy with Axios in 2023

Axios, the JavaScript marvel for crafting HTTP requests, empowers developers with the ability to effortlessly fetch data from web pages in various formats. JSON, CSV, you name it.

Yet, the virtual world's guardians often thwart web scrapers by blocking their path. But fear not! The solution lies in the dynamic duo of Axios and proxies, skillfully navigating you through the web's obstacles.

Join us on this exhilarating journey as we unveil the secrets, featuring real-world examples. We'll unveil the world of free and premium proxies and unveil some ingenious tactics to keep those blockades at bay.

Are you prepared for the adventure?

Prerequisites

Since Axios is the star of our show, we'll be orchestrating web scraping with the symphony of NodeJS.

Before we embark on this quest, ensure that NodeJS and npm are at your service. Create a new sanctuary for your JavaScript endeavors by initiating a project with this command:

mkdir scrapeaxios
cd scrapeaxios
npm init -y

Your journey to unleashing the power of Axios begins with a simple command. Let's equip your project with the tools it needs:

npm install axios

This command will not only fetch Axios but also ensure that its essential dependencies are in place, setting the stage for your project's success.

When venturing into the realm of paid proxies, a portal often guarded by username and password, Axios provides you with a key to unlock the gate. The auth property is your trusty companion in this endeavor.


auth: {
    username: 'your_username',
    password: 'your_password'
}

To wield this power, simply embed the auth property into your script:


const axios = require('axios');

axios.get('https://httpbin.org/ip',
    {
        proxy: {
            protocol: 'http',
            host: 'proxy_host',
            port: portNumber,
            auth: {
                username: 'your_username',
                password: 'your_password'
            },
        },
    }
)
    .then(res => {
        console.log(res.data);
    }).catch(err => console.error(err))

With your credentials in place, you can now boldly traverse the realms of paid proxies, accessing their hidden treasures securely.