Axiosのプロキシ設定でユーザ名/パスワードを設定したい!

0.環境
1.結論
修正前
const proxyUrl = 'http://<proxy.host>:<port>';
const axiosConfig: AxiosRequestConfig = {
proxy: false,
httpAgent: new HttpProxyAgent(proxyUrl),
httpsAgent: new HttpsProxyAgent(proxyUrl),
};
修正後
const proxyUrl = 'http://<proxy.host>:<port>';
const axiosConfig: AxiosRequestConfig = {
proxy: false,
httpAgent: new HttpProxyAgent(proxyUrl),
httpsAgent: new HttpsProxyAgent(proxyUrl),
headers: {
'Proxy-Authentication': `Basic ${Buffer.from('<username>:<password>').toString('base64')}`,
},
};
2.ダメだったパターン:URLに直設定
const proxyUrl = 'http://<username>:<password>@<proxy.host>:<port>;
const axiosConfig: AxiosRequestConfig = {
proxy: false,
httpAgent: new HttpProxyAgent(proxyUrl),
httpsAgent: new HttpsProxyAgent(proxyUrl),
};
3.ダメだったパターン:urlクラスのauthに設定
const proxyOpts = url.parse('http://<proxy.host>:<port>');
proxyOpts.auth = '<username>:<password>';
const axiosConfig: AxiosRequestConfig = {
proxy: false,
httpAgent: new HttpProxyAgent(proxyOpts),
httpsAgent: new HttpsProxyAgent(proxyOpts),
};
proxy: {
host: '<proxy.host>',
port: '<port>',
auth: '<username>:<password>'
},
最後に
案外簡単そうな変更でも、実際に試してみないと分からないことがこの業界にはしばしばあります。
今回はまさにその事例に当たってしまったわけですが、この苦労を苦労のままにしておくのは勿体ないなと。
色々な記事や公式ドキュメントを漁り・組み合わせ・試行錯誤してたどり着いた知見を困っている方に届けられたら幸いです。
