Categories
JavaScript Answers

How to configure Axios to use SSL certificate with JavaScript?

Spread the love

To configure Axios to use SSL certificate with JavaScript, we call axios.get with a https agent object.

For instance, we write

const httpsAgent = new https.Agent({
  rejectUnauthorized: false,
  cert: fs.readFileSync("./usercert.pem"),
  key: fs.readFileSync("./key.pem"),
  passphrase: "YYY",
});

axios.get(url, { httpsAgent });

to create an https.Agent object with the cert certificate, key key file, and the passphrase.

Then we call axios.get with an object with the httpsAgent to use it to make secure requests.

We can also write

const httpsAgent = new https.Agent({
  rejectUnauthorized: false,
  cert: fs.readFileSync("./usercert.pem"),
  key: fs.readFileSync("./key.pem"),
  passphrase: "YYY",
});

const instance = axios.create({ httpsAgent });

to create a new Axios instance with axios.create.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *