我正在尝试从下面提到的API中获取一些信息,而我却遇到了以下错误
const request = require('request');
var options = {
url : 'https://example.com',
}
OR
var options = {
url : 'https://example.com',
agentOptions: {
ca: fs.readFileSync('ca.pem')
}
}
console.log(options)
request(options, function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body );
});
error: { Error: self signed certificate in certificate chain
at Error (native)
at TLSSocket.<anonymous> (_tls_wrap.js:1092:38)
at emitNone (events.js:86:13)
at TLSSocket.emit (events.js:185:7)
at TLSSocket._finishInit (_tls_wrap.js:610:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:440:38) code: 'SELF_SIGNED_CERT_IN_CHAIN' }
样本pem文件
-----BEGIN CERTIFICATE-----
Key
-----END CERTIFICATE-----
我不想使用NODE_TLS_REJECT_UNAUTHORIZED ='0'或拒绝未经授权:false
答案 0 :(得分:0)
可以接受除由签名的证书以外的其他证书 通常允许的证书颁发机构(CA)。这很有用, 例如,当使用自签名证书时。要求 不同的根证书,您可以通过添加来指定签名CA CA的证书文件的内容发送到agentOptions。证书 域提供的内容必须由指定的根证书签名:
var options = {
url : 'https://example.com',
agentOptions: {
ca: fs.readFileSync('ca.pem')
}
}
答案 1 :(得分:0)
这有望很好地工作:
var options = {
url : 'https://example.com',
ca: fs.readFileSync('ca.pem'),
insecure: true,
rejectUnauthorized: false,
}
console.log(options)
request(options, function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body );
});