Axios's Node HTTP adapter accepts an HTTP proxy object with a host and numeric port:

import axios from 'axios';

const response = await axios.get('https://example.com/', {
  adapter: 'http',
  proxy: {
    protocol: 'http',
    host: '203.0.113.10',
    port: 8080,
  },
  timeout: 15000,
});

console.log(response.status);

Replace the documentation endpoint with an HTTP entry from your service or the HTTP proxy list. These examples use Node; browser Axios cannot select the browser's network proxy through these options. The host field takes the address without a URL scheme.

The proxy object's protocol describes its listener. In Axios 1.21.0, the Node HTTP adapter uses CONNECT for an HTTPS target, then negotiates TLS with the origin through the tunnel. Older releases have used different proxy paths, so the installed version matters.

A SOCKS URL needs a SOCKS agent

The HTTP proxy object does not select SOCKS. Use a compatible agent instead:

npm install [email protected] [email protected]
import axios from 'axios';
import { SocksProxyAgent } from 'socks-proxy-agent';

const agent = new SocksProxyAgent('socks5h://203.0.113.10:1080');

try {
  const response = await axios.get('https://example.com/', {
    adapter: 'http',
    httpAgent: agent,
    httpsAgent: agent,
    proxy: false,
    timeout: 15000,
  });
  console.log(response.status);
} finally {
  agent.destroy();
}

proxy: false disables Axios's HTTP proxy handling for the route supplied by the SOCKS agent. Supplying the agent for both HTTP and HTTPS destinations covers both schemes. Here socks5h delegates destination DNS to the SOCKS side; the selected server must support that protocol.

Reading failures

An Axios error can include a response when the website returned an unsuccessful status. Without one, inspect the error code and connection details. A missing module, unsupported protocol, or incompatible Node version may fail before a request reaches the endpoint.

For authenticated HTTP proxies, use the proxy object's auth field with credentials provided by the operator. The destination's authentication settings serve a different party.

Ũhoro ũhĩtĩte