NodeJS中的HTTPS请求 [英] HTTPS request in NodeJS
问题描述
我正在尝试编写一个NodeJS应用程序,该应用程序将使用https包中的请求方法与OpenShift REST API进行通信。以下是代码:
I am trying to write a NodeJS app which will talk to the OpenShift REST API using the request method in the https package. Here is the code:
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做同样的事情时,
But this is giving me an error (status code 500 is returned). When I did the same thing using curl on the command line,
curl -k -X GET https://openshift.redhat.com/broker/rest/api
我收到服务器的正确回复。
I am getting the correct response from the server.
代码中有什么问题吗?
推荐答案
比较哪些标题卷曲和节点发送,我发现添加:
Comparing what headers curl and node sent, i found that adding:
headers: {
accept: '*/*'
}
到期权
修复它。
要查看curl发送的标题,可以使用 -v
参数。
curl -vIX获取https://openshift.redhat.com/broker/rest/api
To see which headers curl sends, you can use the -v
argument.
curl -vIX GET https://openshift.redhat.com/broker/rest/api
在节点中,只需 console.log(req._headers)
在 req.end()
之后。
In node, just console.log(req._headers)
after req.end()
.
快速提示:您可以使用 https.get()
,而不是 https.request()
。它会将方法设置为 GET
,并为您调用 req.end()
。
Quick tip: You can use https.get()
, instead of https.request()
. It will set method to GET
, and calls req.end()
for you.
这篇关于NodeJS中的HTTPS请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!