Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetchError: unable to verify the self-signed certificate along with net::ERR_CERT_AUTHORITY_INVALID with put method

I am using a next js node server as my app. And a ngnix as my https server with self-signed certificate in which my API node server is at behind.

But I am getting a self-signed certificate error.

enter image description here

So, in my next js , I will contact the https server either by fetch and axios. for example.

enter image description here

enter image description here

Is there a easy way on how to get ride of it without buying SSL from real CA?

What I have tried:

This problem couldn't be by pass thru chrome insecure content enabling since it is a server error.

I am guessing this could be achieved from either setting node server / fetch or axios. But I am so new on this kind of problem.

second update

process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0; 

works to get rid of the fetch error:

But now it shown this error with put method:

net::ERR_CERT_AUTHORITY_INVALID

enter image description here

What I have done is to put process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0; on every api call.

For example

   try {
      process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
      const res = await axios.put(url);
     
    } catch (error) {
      console.log(error);
    }

I am still looking for a solution.

like image 786
jason Avatar asked Oct 31 '25 20:10

jason


1 Answers

Try adding httpsAgent in your fetch call

const https = require('https');
const httpsAgent = new https.Agent({
    rejectUnauthorized: false,
  });
const response = await fetch("https://localhost/api", {
 
    // Adding method type
    method: "POST",
 
    // Adding body or contents to send
    body: JSON.stringify(
    {data:"data"}),
     
    // Adding headers to the request
    headers: {
        "Content-type": "application/json; charset=UTF-8"
    },
    agent: httpsAgent,
})

It worked for me

like image 134
Aakash Avatar answered Nov 02 '25 12:11

Aakash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!