使用 axios 时出现 SELF_SIGNED_CERT_IN_CHAIN 错误,但使用 curl 或 postman 时可以通过

我正在对一个端点进行 axios 调用,但此操作会导致 'SELF_SIGNED_CERT_IN_CHAIN' 授权错误。 当我通过 Postman 发送相同的请求时,它可以正常工作。而且,当我通过 curl 发送相同的请求时,它也可以正常工作。 我阅读了其他帖子中的相关信息,设置 'NODE_TLS_REJECT_UNAUTHORIZED' 为 '0',但没有帮助。我还发现有些帖子说这与 npm 有关,要升级它,但那也没有帮助。

这是我的 axios 调用:

const response = await axios({
          method: 'post,
          url: 'https://some-test-url:port#123',
          data: { hello: 'world' },
          agentOptions: new https.Agent({
            key: fs.readFileSync("./key-file.key", 'utf8'),
            cert: fs.readFileSync("./cert-file.pem", 'utf8'),
            rejectUnauthorized: false,
            keepAlive: false,
          })
        });

这是我的 curl 请求:

    curl https://some-test-url:port#123 -X POST -k -v --cert ./cert-file.pem  --key ./key-file.key -d '{"hello": "world"}' -H 'Content-Type: application/json'

这是我收到的错误:

'<html>\r\n<head><title>400 No required SSL certificate was sent</title></head>\r\n<body bgcolor="white">\r\n<center><h1>400 Bad Request</h1></center>\r\n<center>No required SSL certificate was sent</center>\r\n<hr><center>openresty/1.13.6.1</center>\r\n</body>\r\n</html>\r\n'

任何想法我错过了什么?

点赞
用户19618416
用户19618416

你可以在这里看到答案: nodejs - error self signed certificate in certificate chain

只需要设置环境变量即可修复问题:

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;
2022-07-25 14:31:53