node.js - URL 在浏览器中加载,但不在终端中加载(curl 或 Node.js)

标签 node.js xml curl request ssl-certificate

我想向 https://www.bnro.ro/nbrfxrates.xml(罗马尼亚国家银行)发出 GET 请求以获取汇率今天。

虽然 XML 文档在浏览器中加载良好(在 Safari 和 Chrome 中测试),但在终端中却以某种方式失败(使用 Node.js 和curl 检查):

$ curl -vL http://www.bnro.ro/nbrfxrates.xml
* Expire in 0 ms for 6 (transfer 0x7f8a5c009c00)
* Expire in 1 ms for 1 (transfer 0x7f8a5c009c00)
...
* Expire in 5 ms for 1 (transfer 0x7f8a5c009c00)
*   Trying 194.102.208.89...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x7f8a5c009c00)
* Connected to www.bnro.ro (194.102.208.89) port 80 (#0)
> GET /nbrfxrates.xml HTTP/1.1
> Host: www.bnro.ro
> User-Agent: curl/7.64.0
> Accept: */*
> 
< HTTP/1.1 302 Found
< Date: Fri, 14 Jun 2019 11:13:13 GMT
< Location: https://www.bnro.ro/nbrfxrates.xml
< Server: BigIP
< Content-Length: 0
< X-Cache: MISS from HS-F0
< X-Cache-Lookup: MISS from HS-F0:0
< Via: 1.1 HS-F0 (squid/3.4.8)
< Connection: keep-alive
< 
* Connection #0 to host www.bnro.ro left intact
* Issue another request to this URL: 'https://www.bnro.ro/nbrfxrates.xml'
* Expire in 1 ms for 1 (transfer 0x7f8a5c009c00)
* Expire in 0 ms for 1 (transfer 0x7f8a5c009c00)
...
* Expire in 1 ms for 1 (transfer 0x7f8a5c009c00)
*   Trying 194.102.208.89...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x7f8a5c009c00)
* Connected to www.bnro.ro (194.102.208.89) port 443 (#1)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /opt/local/share/curl/curl-ca-bundle.crt
  CApath: none
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (OUT), TLS alert, unknown CA (560):
* SSL certificate problem: unable to get local issuer certificate
* Closing connection 1
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.haxx.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

curl 和我的 Node.js 应用程序都对证书进行了说明。

在我的 Node.js 脚本中,我收到此错误:

{ Error: unable to verify the first certificate
    at TLSSocket.<anonymous> (_tls_wrap.js:1104:38)
    at emitNone (events.js:105:13)
    at TLSSocket.emit (events.js:207:7)
    at TLSSocket._finishInit (_tls_wrap.js:638:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:468:38) code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' }

我尝试使用 Chrome 中的复制为 CURL 命令选项,但这也不起作用。

为什么会发生这种情况?如何解决?

最佳答案

缺少证书包,请在此处了解更多信息 https://curl.haxx.se/docs/sslcerts.html但 TL;DR 是下载 https://curl.haxx.se/ca/cacert.pem并运行

curl -vL --cacert cacert.pem http://www.bnro.ro/nbrfxrates.xml

关于node.js - URL 在浏览器中加载,但不在终端中加载(curl 或 Node.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56597066/

相关文章:

php - 如何检查 PHP CURL SSL 是否有效

javascript - NodeJS 模块函数约定——调用自己的函数

node.js - Winston 没有记录任何日志级别,可能是什么错误?

xml - 在 Go 中解码 XML

php - 通过curl发送终端调用的输出

linux - 如何配置 curl 只显示百分比?

javascript - 使用自定义模块路径时 IntelliJ Javascript "Cannot Find Declaration to go to"

javascript - 如何使我的命令不提及用户?

xml - XML测试脚本/运行器

xml - 是否可以在 XSD 中重命名基本类型