Node.js Rest call to a server that requires a certificate

微笑、不失礼 提交于 2020-01-15 06:06:05

问题


How do a make a rest call from Node.js to a server that requires a certificate for authentication?


回答1:


The solution is to use a custom connection pool, for which you'll need to use a custom Agent.


Here's an example using the standard https module, straight from the documentation:

var options = {
  hostname: 'encrypted.google.com',
  port: 443,
  path: '/',
  method: 'GET',
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
options.agent = new https.Agent(options);

var req = https.request(options, function(res) {
  ...
}

And if you use mikeal's request, you can set the custom agent in the pool option.



来源:https://stackoverflow.com/questions/19709033/node-js-rest-call-to-a-server-that-requires-a-certificate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!