Axios is a popular open-source JavaScript library that facilitates HTTP requests from the browser and a Node.js environment. The tool features the automatic transformation of JSON data, efficient management of HTTP headers, and the ability to cancel requests conveniently.
The following shows how to implement Oxylabs Datacenter and Residential Proxies with Axios.
Axios can be installed using any Javascript package manager such as npm, Yarn, Bower, etc. For example, to install it using npm, you can simply run the following command:
npm install axios
You can also install it using CDN as shown below:
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
If you’re using a different package or an alternative CDN, please refer to Axios’ official documentation.
Axios offers the ability to integrate it with proxy servers. Let’s take a look at the code snippet below:
const browser = require('axios')
browser.get('https://example.com',
{
proxy: {
protocol: 'http',
host: '<host_address>',
port: <port_number>
}
}
).then(result => {
console.log(result.data)
}).catch(error => console.error(error))
The code is pretty straightforward – first, we instantiate a browser object using the Axios library. Then, we’re making a GET request to the website: example.com. Notice, when we’re making the GET request, we’re also passing a dictionary with the proxy information. Inside the dictionary, we are passing the protocol, host, and port to properly identify which proxy to use.
To use Oxylabs' Residential Proxies, we’ll have to slightly modify the above code, updating the information as shown below:
proxy: {
protocol: 'http, https, or socks5',
host: 'pr.oxylabs.io',
port: 7777,
username: 'USERNAME',
password: 'PASSWORD',
}
The host address is now set to pr.oxylabs.io, the port is set to 7777, and the protocol is either HTTP or HTTPS. Also, you’ll have to update the credentials with your own – you can set up your proxy user's credentials in our dashboard.
If you’re using Shared Datacenter Proxies, fill in the information as shown below:
proxy: {
protocol: 'http',
host: 'dc.pr.oxylabs.io',
port: 10000,
username: 'USERNAME',
password: 'PASSWORD',
}
If you’re using Enterprise Dedicated Datacenter Proxies purchased through the sales team, you have to choose the protocol of your acquired proxies. Insert your unique IP address in the host field, and enter 60000 in the port field:
proxy: {
protocol: 'http or socks5',
host: '1.2.3.4',
port: 60000,
username: 'USERNAME',
password: 'PASSWORD',
}
When it comes to Self-Service Dedicated Datacenter Proxies purchased via the Oxylabs’ dashboard, you have to use the port number that corresponds with the sequential number of the IP address from the acquired list. For example, port 8001 will use the first proxy IP from your list:
proxy: {
protocol: 'http or https',
host: 'ddc.oxylabs.io',
port: 8001,
username: 'USERNAME',
password: 'PASSWORD',
}
Note: For additional Oxylabs’ proxy customization options – such as country-specific entry points – refer to Shared Datacenter, Enterprise Dedicated Datacenter, Self-Service Dedicated Datacenter, and Residential Proxies documentations, respectively.
Finally, let’s put the proxy integration to the test and see if the connection was successful. You can navigate to the ip.oxylabs.io website and see if your original IP address was changed with that of a proxy server. Check out the code given below:
const browser = require('axios')
browser.get('https://ip.oxylabs.io',
{
proxy: {
protocol: 'http',
host: 'pr.oxylabs.io',
Port: 7777,
username: 'USERNAME',
password: 'PASSWORD',
}
}
).then(result => {
console.log(result.data)
}).catch(error => console.error(error))
The code above showcases the connection to a Residential Proxy.
Note: make sure to update the code using your own credentials and proxy server information.
Robust Oxylabs’ proxies allow developers to use Axios with enhanced anonymity and security, while protecting them from IP blocks.
If you have any questions regarding Oxylabs proxy server integration with Axios, don’t hesitate to get in touch with our team for more information.
Please be aware that this is a third-party tool not owned or controlled by Oxylabs. Each third-party provider is responsible for its own software and services. Consequently, Oxylabs will have no liability or responsibility to you regarding those services. Please carefully review the third party's policies and practices and/or conduct due diligence before accessing or using third-party services.
How to Build a Web Scraper?
If you’re considering starting web scraping for your business, this article is what you need. Learn the necessary steps of building a web scraper in Python or JavaScript programming languages. You will also find out the most common web scraping challenges and how to deal with them.
Web Scraping with JavaScript and Node.js
Node.js is the engine that runs the JavaScript code without a browser. Web scraping with JavaScript and Node.js is not only easy, it is fast, and the learning curve is very low for those who are already familiar with JavaScript.
How to Make HTTP Requests in Node.js With Fetch API
A step-by-step tutorial on making HTTP requests in Node.js with Fetch API, detailing the actionable code and a comparison to Axios.
Get the latest news from data gathering world
Get Axios proxies just for $10/GB
GET IN TOUCH
General:
hello@oxylabs.ioSupport:
support@oxylabs.ioCareer:
career@oxylabs.ioCertified data centers and upstream providers
Connect with us
Advanced proxy solutions
Resources
Innovation hub