HTTPS 请求忽略 rejectUnauthorized 参数

4
我正在尝试使用nodejs 0.12连接到远程服务器,但是我不断收到SELF_SIGNED_CERT_IN_CHAIN响应。我查看了类似的问题12,但它们的解决方案在我的服务器上无效。
我正在连接到一个由自签名证书设置的我无法控制的测试环境。这是我的请求:
var https = require("https");
var fs = require('fs');

start();

function start()
{
    var listadebancos = 
    {
        language:"es",
        command:"GET_BANKS_LIST",

        merchant:
        {
            apiLogin:"111111111111111",
            apiKey:"11111111111111111111111111",
        },

        test:true,
        bankListInformation:
        {
            paymentMethod:"PSE",
            paymentCountry:"CO"

        }
    };

    var listadebancosString = JSON.stringify(listadebancos);

    var headers = 
    {
        'Content-Type': 'application/json',
        'Content-Length': listadebancosString.length
    };

        var options= {
            host: 'stg.api.payulatam.com',
            rejectUnauthorized: false,
            agent:false,
            path: '/payments-api/4.0/service.cgi',
            method: 'POST',
            cert: fs.readFileSync('./stg.gateway.payulatam.crt'),

        }

        process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

        var req= https.request(options, funcionRespuesta);
        req.write(listadebancosString); 
        req.end();



        function funcionRespuesta(res) 
        {   console.log(res);
        }

    }

我是否遗漏了什么显而易见的东西?


移除并撤销 API 密钥。您需要让证书颁发机构修复证书或告诉您的 HTTP 代码忽略该问题。我不知道如何使用默认的 HTTP 模块来实现这一点;但是,使用 request,您可以传递一个选项 strictSSL: false。此外,https.request 中的 cert 选项是客户端证书。 - Plato
我决定使用另一个库叫做needle,问题得以解决。无论如何,感谢您的建议。 - NicolasZ
兄弟,你的代码里还有一个与财务相关的API密钥,我会帮你编辑掉它,但是在编辑历史记录中,它仍然对所有人可见,永远存在于互联网上,所以赶紧撤销它吧!! - Plato
放心,这是公共测试密钥 :) 万岁! - NicolasZ
1个回答

4

我决定使用一个叫做needle的库来发起请求,这次我成功接收到了响应,没有出现SSL错误。如果有人遇到相同的情况,这是我使用的代码:

var listadebancos = 
{
    "language":"es",
       "command":"GET_BANKS_LIST",
       "merchant":{
          "apiLogin:"111111111111111",
          "apiKey:"11111111111111111111111111",
       },
       "test":false,
       "bankListInformation":{
          "paymentMethod":"PSE",
          "paymentCountry":"CO"
       }
};

};

// var listadebancosString = JSON.stringify(listadebancos);

var headers = 
{
    'Content-Type': 'application/json'
};

    var options = {
        host: 'stg.api.payulatam.com',
        **json:true,**
        path: '/payments-api/4.0/service.cgi',
        method: 'GET',
        headers: headers,
        rejectUnauthorized: false,
        requestCert: true,
        agent: false,
        strictSSL: false,
    }       
    needle
      .post('stg.api.payulatam.com/payments-api/4.0/service.cgi',listadebancos, options, funcionRespuesta)
       .on('end', function() {
        console.log('Ready-o, friend-o.');
      })


    function funcionRespuesta(err, resp, body)
    {
        console.log(err);
        console.log(body);                  
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接