【发布时间】:2020-05-20 12:03:57
【问题描述】:
我目前使用 request 在 node.js 中发出 http 请求。我在某个时候遇到了一个问题,我收到了指示 UNABLE_TO_GET_ISSUER_CERT_LOCALLY 的错误。为了解决这个问题,它设置了rejectUnauthorized。我的请求工作代码如下所示:
var url = 'someurl';
var options = {
url: url,
port: 443,
// proxy: process.env.HTTPS_PROXY, -- no need to do this as request honors env vars
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko',
'Accept-Language': 'en-us',
'Content-Language': 'en-us'
},
timeout: 0,
encoding: null,
rejectUnauthorized: false // added this to prevent the UNABLE_TO_GET_ISSUER_CERT_LOCALLY error
};
request(options, function (err, resp, body) {
if (err) reject(err);
else resolve(body.toString());
});
我想我会尝试使用 async/await 切换到 fetch api,现在我正在尝试使用 node-fetch 来做同样的事情。但是,当我做同样的事情时,我又回到了 UNABLE_TO_GET_ISSUER_CERT_LOCALLY 错误。我读到我需要使用代理并尝试使用代理模块,但我仍然没有任何运气。
根据https://github.com/TooTallNate/node-https-proxy-agent/issues/11 的帖子,我认为以下方法可行:
var options = {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko',
'Accept-Language': 'en-us',
'Content-Language': 'en-us'
},
timeout: 0,
encoding: null
};
var proxyOptions = nodeurl.parse(process.env.HTTPS_PROXY);
proxyOptions.rejectUnauthorized = false;
options.agent = new ProxyAgent(proxyOptions);
const resp = await fetch('someurl', options);
return await resp.text();
但我仍然遇到同样的错误。到目前为止,我能够使用 node-fetch 解决这个问题的唯一方法是在我的环境中设置 NODE_TLS_REJECT_UNAUTHORIZED=0 ,而我真的不想这样做。有人可以帮我看看如何让rejectUnauthorized 与node-fetch 一起工作(大概使用代理,但老实说,我并不关心它被指定为请求的一部分多长时间)。
【问题讨论】:
-
试试隧道模块吧!较新版本的node-https-proxy-agent有问题!您可以使用旧版本 3.x 及以下版本!它会起作用的!否则只需使用隧道模型!这是我的经验以及我如何让它发挥作用!
-
我也更新了问题!它有一个很好的例子,这个包可能很有趣(我在节点隧道上制作的一个包装器)npmjs.com/package/proxy-http-agent
标签: javascript node.js fetch node-fetch