0
我正在测试对API(Splunk)的一些调用。当我以卷曲方式进行呼叫时,它按预期工作。大块事件永远不会在https请求中触发
curl -k -v -u admin:password -d 'search=search error | head 10' -d "output_mode=json" https://192.168.50.16:8089/servicesNS/admin/search/search/jobs/export
这工作正常,并返回一个包含json结果数组的主体。
但是,当我尝试从我的节点应用程序做出相同的请求,我没有身体。在下面的代码中,on chunk事件从不会触发。没有错误;请求似乎正常,我确实得到了一个200头“content-type”:“application/json; charset = utf-8”。那么为什么没有身体/大块?请求方法在这里执行的操作与curl的做法不同吗?据我所知,这些应该是完全相同的要求。
在进行故障诊断时,我制作了一个快速PHP脚本,用于回显POST变量。当我在该脚本中指定此代码时,它运行良好 - 我用PHP输出获得了块。所以我只想找出API为什么可以正确响应来自curl的请求,而不是来自node.js的请求。有什么想法吗?
var https = require('https');
var querystring = require('querystring');
var data = { output_mode:'json', search: 'search error | head 10' };
var datastring = querystring.stringify(data);
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': datastring.length
};
var options = {
hostname: '192.168.50.16',
port: 8089,
path: '/servicesNS/admin/search/search/jobs/export',
method: 'POST',
auth: 'admin:password',
headers: headers
};
var theRequest = https.request(options, function(api_response) {
var responsedata = '';
api_response.on('data', function(chunk) {
console.log('got a chunk');
responsedata += chunk;
});
api_response.on('end', function() {
callback(true, responsedata);
});
});
theRequest.write(datastring);
theRequest.end();
theRequest.on('error', function(e) {
console.error(e);
});
谢谢!