I am trying to download an xml file so I can parse it in nodejs. Here is the test I built via mocha, the test passes but nothing is showing up on the console. What am I doing wrong?
it('Get XML data', function() {
var options = {
hostname: 'stona-app01.us.work.net',
port: 1213,
path: '/solrLive/STONA-STOUS-product-search-idx-en_US/select/?version=2.2&start=2&rows=2&qf=SKU&fl=*&indent=on&onlineFlag=1',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
});
The URL in the code is not reachable so you will not be able to test it.
Your test is asynchronous, so you must define your test to take a done parameter and call it when your test is done:
it('Get XML data', function (done) {
var options = {
hostname: 'stona-app01.us.work.net',
port: 1213,
path: '/solrLive/STONA-STOUS-product-search-idx-en_US/select/?version=2.2&start=2&rows=2&qf=SKU&fl=*&indent=on&onlineFlag=1',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
done(); // We're done!
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
done(e); // Pass the error to Mocha.
});
});
Related
I want to make multiple HTTP requests with different bodies each time, using loop or something similar. Currently I use the following code for a single request which works fine:
var http = require('http');
var post_req = null,
post_data = JSON.stringify(require('./resources/example.json'));
var post_options = {
hostname: 'example.lk',
port : '80',
path : '/example',
method : 'POST',
headers : {
'Content-Type': 'application/json',
'Authorization': 'Cucmlp1qdq9CfA'
}
};
post_req = http.request(post_options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ', chunk);
});
});
post_req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
post_req.write(post_data);
post_req.end();
How can I use this code to preform multiple calls?
You can use async to call several `http
var async = require('async');
var http = require('http');
var post_data = [ data1, data2, data2]; //array of data, you want to post
//asynchronously, loop over array of data, you want to push
async.each(post_data, function(data, callback){
var post_options = {
hostname: 'example.lk',
port : '80',
path : '/example',
method : 'POST',
headers : {
'Content-Type': 'application/json',
'Authorization': 'Cucmlp1qdq9CfA'
}
};
post_req = http.request(post_options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ', chunk);
});
res.on('end', function () {
callback();
});
});
post_req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
post_req.write(data); //posting data
post_req.end();
}, function(err){
console.log('All requests done!')
});
I am trying to make a PUT request to Firebase with NodeJS. When I make the following request, the response hangs until it times-out. No error message makes it difficult to debug--
var querystring = require('querystring');
var https = require('https');
var data = querystring.stringify({
'foo': 'bar'
});
var options = {
host: 'my-app.firebaseio.com',
port: '80',
path: '/datetimes.json?auth=password',
method: 'PUT'
var req = https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
req.write(data);
req.end();
My guess is that there is a problem with the Firebase authentication, or else I'd get an error. But this does work--
https.get({ host: 'my-app.firebaseio.com', path: '/datetimes.json?auth=password' }, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
}).on('error', function(e) {
console.error(e);
});
So that makes me think maybe the password is okay and I'm just using https.request incorrectly?
[I cannot use any NPM libraries (i.e. request) because this needs to work with AWS Lambda.]
I am getting truncated response in my node service.
I am using http request.Please notify me any error in this code.
I want to get response without losing data.
My Code :
var config = require('./configuration')
exports.postDataDetails = function postDataDetails(http,url,port,path,inputParam,callback){
var postData = inputParam;
var options = {
hostname: url+":",
port: port,
path: path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + new Buffer(config.headerUsername + ':' + config.headerPassword).toString('base64'),
'Content-Length': Buffer.byteLength(postData,'utf8')//postData.length
}
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
return callback(chunk);
});
res.on('end', function() {
console.log('No more data in response.');
return callback(JSON.stringify({status : "No more data in response."}));
})
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
return callback(JSON.stringify({status : "error"}));
});
// write data to request body
req.write(postData);
req.end();
}
How to fix that problem.
Thanks.
I've got a code that prints on my screen the result of a get. The excerpt of the code that does this is the following:
http.get(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.on('data', function(chunk) {
process.stdout.write(chunk);
});
}).on('error', function (e) {
console.log('ERROR: ' + e.message);
});
I want it to write the results to a file instead. How can I do it? In which folder should I save the file, in order to avoid any possible windows security blocks?
Thanks!
Use nodes file system
var fs = require('fs');
http.get(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.on('data', function(chunk) {
fs.writeFile('message.txt', chunk, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
});
}).on('error', function (e) {
console.log('ERROR: ' + e.message);
});
i am using node.js i write a small code
var http = require('http');
var options = {
hostname: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
i have to send post requests for starting i am making get requests that why no http.get().in this request i am getting 404 response code which is also an error.but not captured in req.on('error') .i want to know which kind of error this methd captures ??
From http.request documentation:
If any error is encountered during the request (be that with DNS resolution, TCP level errors, or actual HTTP parse errors) an 'error' event is emitted on the returned request object.
So probably that event is only emitted when there is an error at the protocol/connection level.
You can then process HTTP errors on the connect call:
var req = http.request(options, function(res) {
if (res.statusCode == "404"){ ... }
else if (...) {
...
} else { // everything ok
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
...
}
});