Making a REST call in Node.js that requires client

2019-04-06 16:18发布

Is there a way to make a rest call that requires a client certificate for authentication through Node.js ?

1条回答
姐就是有狂的资本
2楼-- · 2019-04-06 16:54

Yes, you can do that quite simply, here done using a regular https request;

var https = require('https'),                  // Module for https
    fs =    require('fs');                     // Required to read certs and keys

var options = {
    key:   fs.readFileSync('ssl/client.key'),  // Secret client key
    cert:  fs.readFileSync('ssl/client.crt'),  // Public client key
    // rejectUnauthorized: false,              // Used for self signed server
    host: "rest.localhost",                    // Server hostname
    port: 8443                                 // Server port
};

callback = function(response) {
  var str = '';    
  response.on('data', function (chunk) {
    str += chunk;
  });

  response.on('end', function () {
    console.log(str);
  });
}

https.request(options, callback).end();
查看更多
登录 后发表回答