Axios response Error: certificate has expired
Asked Answered
A

3

7

I'm using axios to send request to diro to create an user with endpoint /user/create.

However, I keep getting an error like this:

Error response: { Error: certificate has expired
    at TLSSocket.onConnectSecure (_tls_wrap.js:1055:34)
    at TLSSocket.emit (events.js:198:13)
    at TLSSocket.EventEmitter.emit (domain.js:448:20)
    at TLSSocket._finishInit (_tls_wrap.js:633:8)

Here is my request:

const DIRO_API_KEY = ******
createUserToDiro = ({
  phone,
  first_name,
  last_name,
  birth_date,
  mcc_code
}) => {
  const mobile = phone;
  const firstname = first_name;
  const lastname = last_name;
  const dob = formatDiroDob(birth_date);
  const mcc = mcc_code;
  axios.post('https://api.dirolabs.com/user/create'), {
    firstname,
    lastname,
    dob,
    mobile,
    mcc,
    apikey: DIRO_API_KEY
  })
  .then(rs => console.log('Success response:', rs))
  .catch(err => console.log('Error response:',err));

};

What is causing this issue and is there any way to fix it ?

Andromache answered 6/11, 2019 at 6:33 Comment(1)
Certificate on the api end is expired. I think you should contact diro's tech support or wait until issue will be resolved.Nakasuji
L
13

Axios library error clearly mentioned that the certificate has expired.

Please ask diro to renew SSL certificate.

Another way we can skip checking for SSL in Axios npm library as follows

const axios = require('axios');
const https = require('https');

// At request level
const agent = new https.Agent({
    rejectUnauthorized: false
});

// Make a request for a user
axios.get('/user/create', { httpsAgent: agent })
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .finally(function () {
    // always executed
  });

This works.

Luing answered 5/6, 2020 at 9:26 Comment(4)
I have this issue in a VSCode extension that uses Axios to do some service calls. The certificates of all certs in the chain are still valid and none of them is expired still only Windows users who use the extension do get this message while Linux and Mac users do not get this message. This therefore seems like a bug inside Axios or https IMO. My current guess is that it's related to TLS 1.3 only being supported by the target endpointHuba
I have encountered this issue on Ubuntu 16.04. Yet same Axios request is working fine under Ubuntu 20.04. Not sure how to fix it on Ubuntu 16.04 without using the solutions provided here.Hermeneutics
@Hermeneutics It turned out that in my case Let's Encrypt changed their certificate chain a bit in the last year and their root certifciate is now not a real root certificate but one that points to the old, outdate one AFAIK. Axios will use the default truststore if present to perform the TLS handshake which on Linux via OpenSSL and Mac via LibreSSL libraries is usually available with the updated trustchain. On Windows however such a default SSL library usually does not exist and thus the whole trustchain can't be verified correctly and will at some point use the old, outdated root cert.Huba
did not work for meFavouritism
M
8

shorter way, using this environment variable NODE_TLS_REJECT_UNAUTHORIZED=0

Martimartial answered 22/1, 2022 at 7:16 Comment(0)
T
0

You can use insecureHTTPParser: true in the config parameter, that bypassess the check.

axios.get('/user/create', { insecureHTTPParser: true })
Trite answered 28/3 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.