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);
});
謝謝!