0

I have a simple .NET Core WebAPI with no authentication. I added Cors with default policy. I have no problem connecting and fetching data from my React website or Postman (everything runs locally on my machine). Now I'm trying to fetch data from that API in super simple node application and I'm getting this error:

file:///Users/aw/Projects/TestNodeApp/node_modules/node-fetch/src/index.js:94
            reject(new FetchError(`request to ${request.url} failed, reason: ${error.message}`, 'system', error));
                   ^

FetchError: request to https://localhost:5001/api/teams failed, reason: self signed certificate
    at ClientRequest.<anonymous> (file:///Users/aw/Projects/TestNodeApp/node_modules/node-fetch/src/index.js:94:11)
    at ClientRequest.emit (node:events:394:28)
    at TLSSocket.socketErrorListener (node:_http_client:447:9)
    at TLSSocket.emit (node:events:394:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  type: 'system',
  errno: 'DEPTH_ZERO_SELF_SIGNED_CERT',
  code: 'DEPTH_ZERO_SELF_SIGNED_CERT',
  erroredSysCall: undefined
}

This is my whole node application:

import fetch from 'node-fetch';

async function fetchTeams() {
  const response = await fetch('https://localhost:5001/api/teams', { method: 'GET' });
  const data = await response.json();
  return data;
}

(async () => {
  console.log('Process started');
  const teams = await fetchTeams();
  console.log(teams);
})().finally(() => {
  console.log('Process finished');
});

What does it mean? What Am I missing?

Btw. It works fine, when I'm fetching Github API, like this:

async function fetchGithub() {
  const response = await fetch('https://api.github.com/users/Microsoft');
  const data = await response.json();
  return data;
}

So I assume, something is missing in the API. Something that my React website doesn't need, that node app needs.

Thanks for help!

Adam Wojnar
  • 473
  • 6
  • 19
  • 1
    https://stackoverflow.com/questions/10888610/ignore-invalid-self-signed-ssl-certificate-in-node-js-with-https-request – Will Sep 18 '21 at 18:11

2 Answers2

2

You can use this command to set the NODE_TLS_REJECT_UNAUTHORIZED environment variable:

export NODE_TLS_REJECT_UNAUTHORIZED=0
starball
  • 20,030
  • 7
  • 43
  • 238
0

Try trusting the self signed certificate with dotnet dev-certs

dotnet dev-certs https --trust

For more details please visit this documentation page.

maiksch
  • 141
  • 7