最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Self signed certificate error when fetching API locally - Stack Overflow

programmeradmin1浏览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('');
  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!

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./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!

Share Improve this question asked Sep 18, 2021 at 17:27 Adam WojnarAdam Wojnar 5451 gold badge8 silver badges23 bronze badges 1
  • 1 stackoverflow./questions/10888610/… – Will Commented Sep 18, 2021 at 18:11
Add a ment  | 

4 Answers 4

Reset to default 6

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

export NODE_TLS_REJECT_UNAUTHORIZED=0

I opened my node start file/js file where you are calling API and put there the following line of code:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

If you want to apply this rule only to a test/development environment, you can try doing this:

if ("development" == app.get("env")) {
  process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
}

If you're using next.js

"scripts": {
    "dev": "NODE_TLS_REJECT_UNAUTHORIZED=0 next dev",
    ...
},

Try trusting the self signed certificate with dotnet dev-certs

dotnet dev-certs https --trust

For more details please visit this documentation page.

发布评论

评论列表(0)

  1. 暂无评论