Radaeld
Radaeld

Reputation: 315

Getting ERR_CERT_AUTHORITY_INVALID with axios

I'm trying to do a post request via https with vue-axios. However, since i'm using a self-signed certificate that i created, i'm getting the following error:

net::ERR_CERT_AUTHORITY_INVALID

Upon searching i found that most people solve this by doing the following

const instance = axios.create({
  httpsAgent: new https.Agent({  
    rejectUnauthorized: false
  })
});
instance.get('https://something.com/foo');

// At request level
const agent = new https.Agent({  
  rejectUnauthorized: false
});
axios.get('https://something.com/foo', { httpsAgent: agent });

I tried both option but didn't have any success with them. I used the npm https module for the https.Agent.

Does anyone know how to solve this problem? or should I just change from axios to other modules?

edited:

the piece of code I'm running with the error at the moment:

const axiosInstance = axios.create({
  baseURL: 'https://localhost:5000',
  httpsAgent: new https.Agent({
    rejectUnauthorized: false
  }),
});
axiosInstance.post('/user', LoginRequest, 
  { headers: { 'Content-Type': 'application/json' } })
    .then(response => this.assignLogin(response.data));

tried to change to a module named needle and use https but had the same error:

needle:

      const headers = { 'Content-Type': 'application/json' };
      const options = {
          method: 'POST',
          headers: headers,
          rejectUnauthorized: false,
          requestCert: true,
          agent: false,
          strictSSL: false,
      }
      needle.post('https://localhost:5000/user', LoginRequest, options).on('end', function() { })

https:

const options = {
         hostname: 'localhost',
         port: 5000,
         path: '/user',
         strictSSL: false,
         rejectUnauthorized: false,
         secureProtocol: 'TLSv1_method',
         method: 'POST',
         headers: {
           'Content-Type': 'application/json',
         },
       };
       const req = https.request(options, (res) => {
         console.log('statusCode:', res.statusCode);
         console.log('headers:', res.headers);

         res.on('data', (d) => {
           this.assignLogin(d);
         });
       });
       req.on('error', (e) => {
         console.error(e);
       });
       req.write(LoginRequest);
       req.end();

Upvotes: 18

Views: 29333

Answers (1)

ofri cofri
ofri cofri

Reputation: 1026

Since you mention that you are "using a self-signed certificate that you created", I guess that you are using this for local development tests. I had a similar issue, when testing locally in Chrome.

As this error message (net::ERR_CERT_AUTHORITY_INVALID) is a way of Chrome blocking a URL with an "unsafe" certificate, you need to solve this issue through Chrome, telling it that you trust the certificate.
The solution I use is the old one thisisunsafe. (ONLY USE THIS SOLUTION IF YOU REALLY TRUST THE CERTIFICATE, I.E., IT'S YOUR OWN CERTIFICATE):

SOLUTION: Just open a tab in Chrome, try to open a URL with your server address (in your case, https://localhost:5000/). Chrome will display a warning message, so you click anywhere in the window, and type thisisunsafe. Chrome will now allow access to this certificate. When you reload the client again and try to request the server, it will work.

Upvotes: 30

Related Questions