How to Configure Axios Proxy Authorization Headers
This article provides a complete guide on how to configure proxy
authentication in the Axios HTTP client when operating behind corporate
proxies. You will learn how to use Axios's built-in proxy configuration,
how to handle HTTPS traffic using dedicated proxy agents like
https-proxy-agent, and how to manually construct custom
Proxy-Authorization headers for complex network
environments.
1. Using Built-in Axios Proxy Settings
For standard HTTP proxy requests, Axios provides a native
proxy configuration object. When you supply the
auth property containing a username and password, Axios
automatically encodes the credentials in Base64 and attaches the
Proxy-Authorization: Basic <base64-credentials>
header to the request.
const axios = require('axios');
axios.get('http://example.com/api/data', {
proxy: {
protocol: 'http',
host: 'proxy.corporate.local',
port: 8080,
auth: {
username: 'corporate_user',
password: 'corporate_password'
}
}
})
.then(response => console.log(response.data))
.catch(error => console.error('Request failed:', error.message));2.
Handling HTTPS Corporate Proxies with
https-proxy-agent
The native Axios proxy object often fails when tunneling
requests over HTTPS because of how the Node.js HTTP runtime handles the
CONNECT method. To reliably pass proxy authentication over
HTTPS in a corporate environment, use the https-proxy-agent
library.
Installation
npm install https-proxy-agentImplementation
Include the authentication credentials directly in the proxy
connection URL, and pass the agent to Axios via the
httpsAgent config option:
const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');
const proxyHost = 'proxy.corporate.local';
const proxyPort = 8080;
const username = encodeURIComponent('corporate_user');
const password = encodeURIComponent('corporate_password');
// Construct the proxy URL with credentials
const proxyUrl = `http://${username}:${password}@${proxyHost}:${proxyPort}`;
const httpsAgent = new HttpsProxyAgent(proxyUrl);
const client = axios.create({
httpsAgent,
proxy: false // Disables Axios's default proxy handling
});
client.get('https://api.external-service.com/data')
.then(response => console.log(response.data))
.catch(error => console.error('HTTPS Proxy Request Failed:', error.message));3. Manually Setting the Proxy-Authorization Header
If your corporate proxy requires custom headers or token-based proxy authentication (such as Bearer or NTLM tokens), you can manually compute and inject the header into the agent configuration or request headers.
Basic Authentication Example
const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');
const username = 'corporate_user';
const password = 'corporate_password';
const base64Auth = Buffer.from(`${username}:${password}`).toString('base64');
const httpsAgent = new HttpsProxyAgent('http://proxy.corporate.local:8080', {
headers: {
'Proxy-Authorization': `Basic ${base64Auth}`
}
});
axios.get('https://api.external-service.com/data', {
httpsAgent,
proxy: false
})
.then(response => console.log(response.data))
.catch(error => console.error(error.message));4. Corporate Environment Considerations
- SSL/TLS Inspection: Corporate proxies often perform
SSL inspection using custom Certificate Authorities (CAs). If you
encounter
UNABLE_TO_VERIFY_LEAF_SIGNATUREorSELF_SIGNED_CERT_IN_CHAINerrors, provide the corporate root certificate via the agent:const fs = require('fs'); const httpsAgent = new HttpsProxyAgent('http://user:pass@proxy:8080', { ca: fs.readFileSync('/path/to/corporate-ca.pem') }); - Special Characters in Passwords: Always use
encodeURIComponent()on your username and password when formatting proxy connection strings to prevent URL parsing errors. - Environment Variables: For scalable setups, use
process.env.HTTPS_PROXYorprocess.env.HTTP_PROXYto populate proxy configuration rather than hardcoding credentials into source code.