I'm testing some calls to an API (Splunk). When I do the call in curl, it works as expected.
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
This works correctly and returns a body containing a json array of results.
But when I try to make the same request from my node app, I get no body. In the code below, the on chunk event never fires. No errors; request appears to go off correctly and I do get a 200 back with a header "content-type":"application/json; charset=utf-8". So why no body/chunks? Does the request method do something differently here than what curl would do? As far as I can tell, these should be the exact same request.
While troubleshooting, I make a quick PHP script that just echo'd back the POST vars. When I pointed this code at that script instead, it worked fine - I got chunks back with my PHP output. So I'm left to try to figure out why the API might respond correctly to a request from curl, but not one from node.js. Any thoughts?
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);
});
Thanks!
Try using the request module. It makes working with http(s) much easier in node. Check out the example below and see if you get any response from the server
var request = require('request')
var postData = { output_mode:'json', search: 'search error | head 10' };
var options = {
url: 'https://192.168.50.16:8089/servicesNS/admin/search/search/jobs/export',
method: 'POST',
auth: {
user: 'admin',
pass: 'password',
},
form: postData
};
var r = request(options, function(err, res, body) {
if (err) {
console.dir(err)
return
}
console.dir('status code', res.statusCode)
console.dir(body)
})
You can also pipe the request and listen to the data events
var options = ... // look above
var r = request(options)
r.on('data', function(data) {
console.log('request data received')
})