我正在尝试编写一个 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);
});
但这给了我一个错误(返回状态代码 500)。当我在命令行上使用curl 做同样的事情时,
curl -k -X GET https://openshift.redhat.com/broker/rest/api
我从服务器得到了正确的响应。
代码有什么问题吗?
比较curl和node发送的标头,我发现添加了:
headers: {
accept: '*/*'
}
to options
修复。
要查看curl发送了哪些标头,您可以使用-v
争论。
curl -vIX GET https://openshift.redhat.com/broker/rest/api
在节点中,只需console.log(req._headers)
after req.end()
.
快速提示:您可以使用https.get()
, 代替https.request()
。它将方法设置为GET
,并调用req.end()
为你。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)