2

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.

James Z
  • 12,209
  • 10
  • 24
  • 44
jason
  • 133
  • 2
  • 8
  • 2
    Please post relevant code as text and not images. – ewokx Feb 23 '22 at 04:25
  • Does this answer your question? [Ignore invalid self-signed ssl certificate in node.js with https.request?](https://stackoverflow.com/questions/10888610/ignore-invalid-self-signed-ssl-certificate-in-node-js-with-https-request) – ewokx Feb 23 '22 at 04:26
  • not yet, please , seems this only work with fetch error. But will keep looking for solution. thanks – jason Feb 23 '22 at 05:01

1 Answers1

2

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

Aakash
  • 21
  • 2