Taylor Cox
Taylor Cox

Reputation: 21

Using an SSL certificate (pem file) in JavaScript

Writing a program to pull data from the FAA's AIDAP database. They sent me a security certificate as a .p12 file I have converted to a .pem. Looking for guidance for an implementation on how to load this in my code. When I run the code I have now, I just get "access forbidden"

    var request = require('request');
request('https://www.aidap.naimes.faa.gov/aidap/XmlNotamServlet', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Show the HTML for the website
  }
})

I am using the Request npm package as an http client, have also experimented with xmlhttprequest as te data I need is html.

Note: I only have the security certificate and a passphrase they gave me to log into the certificate. When I go to the website in Chrome with the certificate installed on Chrome, the website url works. From my javacscript IDE, access forbidden.

Looking for guidance on how to implement the .pem certificate

Upvotes: 2

Views: 7879

Answers (1)

Cristian Smocot
Cristian Smocot

Reputation: 666

Try this code:

var request = require('request');
var fs      = require('fs');
var path    = require('path');
var pemFile = path.resolve(__dirname, 'ssl/certificate.pem');

var options = {
    url        : 'https://www.aidap.naimes.faa.gov/aidap/XmlNotamServlet',
    passphrase : 'password',
    ca         : fs.readFileSync(pemFile) //reading the pem file
};

request.get(options, function (error, response, body) {
    if (!error && response.statusCode === 200) {
        console.log(body); // Show the HTML for the website
    }
});

More documentation on using the request library, regarding TLS/SSL protocol here:

https://github.com/request/request#tlsssl-protocol

Upvotes: 2

Related Questions