node.js - Chunk event never fires in https request -


i'm testing calls api (splunk). when call in curl, works 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 , returns body containing json array of results.

but when try make same request node app, no body. in code below, on chunk event never fires. no errors; request appears go off correctly , 200 header "content-type":"application/json; charset=utf-8". why no body/chunks? request method differently here curl do? far can tell, these should exact same request.

while troubleshooting, make quick php script echo'd post vars. when pointed code @ script instead, worked fine - got chunks php output. i'm left try figure out why api might respond correctly request curl, not 1 node.js. 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 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 request module. makes working http(s) easier in node. check out example below , see if response 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 pipe request , listen data events

var options = ... // above var r = request(options) r.on('data', function(data) {    console.log('request data received') }) 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -