通过 HTTPS 抛出证书问题将 Node.JS 和 Unirest 用于 REST 客户端 [英] Using Node.JS and Unirest for REST client over HTTPS throwing certificate issue

查看:54
本文介绍了通过 HTTPS 抛出证书问题将 Node.JS 和 Unirest 用于 REST 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Unirest 在 Node.JS 中创建我的第一个 REST 客户端.它旨在与通过端口 443 通过 Nginx 代理的 Flask RestPlus 服务器进行通信.我可以在 Chrome 中访问 Swagger 文档而没有 SSL 问题.SSL 证书不是自签名的,而是通过 Digicert 颁发的,并且在我工作的地方内部.当我尝试使用 Unirest 'get' 请求时,我收到 unable to get local issuer certificate.我可以将 'strictSSL(false)' 函数添加到我的 api 调用中,错误就会消失,但我不想关闭 SSL 检查.我在谷歌上到处寻找答案,但没有找到任何东西.任何帮助,将不胜感激.我在 Windows 10 环境中运行 Node.JS 进行开发/测试,但一旦完成,代码将部署到 Cloud Foundry/Linux 环境中.

Node.JS 代码:unirest.get("https://path/to/api/call").headers(headers).end(function (res) { console.log(res); });

给出的错误:

<块引用>

{ 错误:{ 错误:无法获得本地颁发者证书在错误(本机)在 TLSSocket.(_tls_wrap.js:1062:38)在emitNone (events.js:86:13)在 TLSSocket.emit (events.js:185:7)在 TLSSocket._finishInit (_tls_wrap.js:586:8)在 TLSWrap.ssl.onhandshakedone (_tls_wrap.js:416:38) 代码:>'UNABLE_TO_GET_ISSUER_CERT_LOCALLY' } }

解决方案

我找到了解决方案!使用 ssl-root-cas 模块并将以下代码添加到我的 rest-client 文件的顶部,问题已解决.

var rootCas = require('ssl-root-cas/latest').create();rootCas.addFile(__dirname + '/path/to/ca.pem');require('https').globalAgent.options.ca = rootCas;

如果遇到同样的问题,希望其他人会觉得这很有用.

I am attempting to create my first REST client in Node.JS using Unirest. It is intended to communicate with a Flask RestPlus server that is proxied through Nginx over port 443. I can access the Swagger docs in Chrome with no SSL issues. The SSL cert is not self-signed, and is issued through Digicert and is internal to where I work. When I attempt 'get' requests using Unirest I am getting a unable to get local issuer certificate. I can add the 'strictSSL(false)' function to my api call and the error goes away, but I would prefer not to turn off SSL checking. I have looked all over Google for an answer, but have not been able to dig anything up. Any help would be appreciated. I am running Node.JS in a Windows 10 envir0nment for dev/testing, but once finished the code will be deployed into a Cloud Foundry/Linux environment.

Node.JS code: unirest.get("https://path/to/api/call").headers(headers).end(function (res) { console.log(res); });

Error that is given:

{ error: { Error: unable to get local issuer certificate at Error (native) at TLSSocket. (_tls_wrap.js:1062:38) at emitNone (events.js:86:13) at TLSSocket.emit (events.js:185:7) at TLSSocket._finishInit (_tls_wrap.js:586:8) at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:416:38) code: >'UNABLE_TO_GET_ISSUER_CERT_LOCALLY' } }

解决方案

I was able to find the solution this! Using the ssl-root-cas module and adding the following code to the top of my rest-client file the issue was resolved.

var rootCas = require('ssl-root-cas/latest').create();
rootCas.addFile(__dirname + '/path/to/ca.pem');
require('https').globalAgent.options.ca = rootCas;

Hopefully someone else will find this useful if experiencing the same issue.

这篇关于通过 HTTPS 抛出证书问题将 Node.JS 和 Unirest 用于 REST 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆