NodeJS中的HTTPS请求

ssb*_*ssb 53 rest https node.js openshift

我正在尝试编写一个NodeJS应用程序,它将使用https包中的请求方法与OpenShift REST API进行通信.这是代码:

var https = require('https');

var options = {
  host: 'openshift.redhat.com',
  port: 443,
  path: '/broker/rest/api',
  method: 'GET'
};

var req = https.request(options, function(res) {
  console.log(res.statusCode);
  res.on('data', function(d) {
    process.stdout.write(d);
  });
});
req.end();

req.on('error', function(e) {
  console.error(e);
});
Run Code Online (Sandbox Code Playgroud)

但是这给了我一个错误(返回状态代码500).当我在命令行上使用curl做同样的事情时,

curl -k -X GET https://openshift.redhat.com/broker/rest/api
Run Code Online (Sandbox Code Playgroud)

我从服务器得到了正确的答复.

代码有什么问题吗?

Min*_*God 47

比较标题curl和节点发送的内容,我发现添加:

headers: {
    accept: '*/*'
}
Run Code Online (Sandbox Code Playgroud)

options固定它.


要查看curl发送的标头,您可以使用该-v参数.
curl -vIX GET https://openshift.redhat.com/broker/rest/api

在节点中,就console.log(req._headers)在之后req.end().


快速提示:您可以使用https.get(),而不是https.request().它将设置方法GET,并呼吁req.end()你.