Axios is one of the most popular HTTP clients for JavaScript thanks to its versatility, flexibility, and powerful Promise-based API. One of the great things about Axios is that it natively supports HTTP and HTTPS proxies. This allows you to route your HTTP requests through a proxy server, which can be useful for bypassing network restrictions, increasing security, or improving performance.
However, Axios does not natively support SOCKS proxies. You can still use them, but you need an external library such as socks-proxy-agent
.
Follow this tutorial and learn how to use SOCKS proxies with Axios. But first, let’s start with a bit of theory.
What Is SOCKS Proxy?
A SOCKS proxy is a proxy server that uses the SOCKS protocol to route your data over the Internet. In detail, a SOCKS server proxies TCP connections to an arbitrary IP address. Note that SOCKS also supports authentication, so that only authorized users can access the proxy server. Also, it provides UDP forwarding.
Simply put, the SOCKS proxy acts as an intermediary between the client and the server, forwarding incoming web requests from the client to the server and forwarding the server’s responses back to the client. For example, this allows the client to make requests to servers it would not otherwise be able to access due to network restrictions, such as firewalls or censorship.
What Is the Difference Between an HTTP Proxy and a SOCKS Proxy?
The main difference between an HTTP proxy and a SOCKS proxy is the types of traffic they can handle. An HTTP proxy is specifically designed to handle HTTP traffic. On the contrary, a SOCKS proxy can handle any type of web traffic, including HTTP, HTTPS, FTP, and other protocols. Also, HTTP proxies typically operate at the application layer of the OSI model, while SOCKS proxies operate at the transport layer.
So, SOCKS proxies are more versatile than HTTP proxies and can adapt to a wider range of scenarios. At the same time, they may be more complex to configure and use.
Configuring a SOCKS Proxy in Axios
It is time to see a real-world example of how to configure a SOCKS proxy with Axios. Since the Tor project comes with a SOCKS proxy you can access locally to route your web traffic through the Tor network, let’s see how to use the Tor SOCKS proxy in Axios. Note that you can easily extend this example to any other SOCKS proxy.
First, you need to install socks-proxy-agent
. This library allows you to connect to a SOCKS proxy in JavaScript. Add socks-proxy-agent
to your project’s dependencies with:
npm i socks-proxy-agent
Once you have installed socks-proxy-agent
, you can use it to create an agent that acts as a SOCKS proxy for Axios. Here is an example of how to do it with the Tor SOCKS proxy:
import axios from "axios" // on Node.js const axios = require('axios') import { SocksProxyAgent } from "socks-proxy-agent" // on Node.js: const SocksProxyAgent = require('socks-proxy-agent') // define the Tor SOCKS proxy agent const torProxyAgent = new SocksProxyAgent("socks://127.0.0.1:9050"); // perform a GET request over the Tor SOCKS proxy const response = await axios.request({ url: "https://your-target-domain.com/", method: "GET", httpsAgent: torProxyAgent, httpAgent: torProxyAgent, })
The SocksProxyAgent
constructor takes the SOCKS proxy URL and returns an agent that can be used with Axios. In the case of authenticated SOCKS proxies, follow the URL syntax below:
"socks://<USERNAME>:<PASSWORD>@<PROXY_HOST>:<PROXY_PORT>"
Then, pass torProxyAgent
to the Axios request()
function to perform the HTTP request over the SOCKS proxy you configured. Note that you can use torProxyAgent
also with Axios’ get()
, post()
, put()
and similar functions:
const response = await axios.get("https://your-target-domain.com/", { httpsAgent: agent, httpAgent: agent, })
You can also set the SOCKS proxy globally on your Axios instance with:
// globally configure a SOCKS proxy const axiosInstance = axios.create({ httpsAgent: torProxyAgent, httpAgent: torProxyAgent })
With this configuration, all requests made using the Axios instance will be routed through the Tor SOCKS proxy.
Congrats! You know now how to use a SOCKS proxy server in Axios.
Conclusion
In this article, you learned what a SOCKS proxy is, how it differs from an HTTP proxy, and how to adopt a SOCKS proxy in Axios. In detail, you learned that Axios does not support SOCKS proxies natively. However, thanks to the socks-proxy-agent
library you can easily configure Axios to use a SOCKS proxy. Specifically, here you saw how to use the Tor SOCKS proxy in Axios.
Thanks for reading! I hope you found this article helpful.