2015-04-06 25 views
3

我試圖使用nodejs 0.12連接到遠程服務器,並且我不斷收到響應SELF_SIGNED_CERT_IN_CHAIN。我看過類似的問題12,但不知何故他們的解決方案不能在我的服務器上工作。https.request忽略rejectUnauthorized

我正在連接到我的控制之外的測試環境,該環境設置了自簽名證書。這是我的要求:

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); 
     } 

    } 

我是否缺少明顯的東西?

+0

刪除並撤銷api密鑰。您需要讓證書頒發者修復證書或告訴您的http代碼忽略該問題。我不知道如何通過股票http模塊來做到這一點;但是[請求](https://github.com/request/request),您可以傳遞選項'strictSSL:false'.also https.request中的cert選項是CLIENT證書。 – Plato

+0

我決定使用另一個名爲needle的庫,它解決了這個問題。無論如何,感謝您的建議 – NicolasZ

+0

老兄你仍然在你的代碼中有一個與財務相關的api密鑰,我將爲你編輯它,但它仍然可以在編輯歷史中永遠在互聯網上看到,所以REVOKE儘快完成! – Plato

回答

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);     
    }