I have a NodeJS client that is similar to
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();
Is there any way to make the data element in the req.write gzipped at all?
To send a compressed request, compress the data into a zlib buffer, then write that as your output:
var zlib = require('zlib');
var options = {
hostname: 'www.example.com',
port: 80,
path: '/upload',
method: 'POST',
headers: {'Content-Encoding': 'gzip'} // signal server that the data is compressed
};
zlib.gzip('my data\ndata\n', function (err, buffer) {
var req = http.request(options, function(res) {
res.setEncoding('utf8');// note: not requesting or handling compressed response
res.on('data', function (chunk) {
// ... do stuff with returned data
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write(buffer); // send compressed data
req.end();
});
That should send a gzipped request to the server and get a non-compressed response back. See the other answers to this question for handling a compressed response.
You can gzip a stream using the native zip library: http://nodejs.org/api/zlib.html
var zlib = require('zlib');
var gunzip = zlib.createGunzip();
var options = {
hostname: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST',
headers: {'Accept-Encoding': 'gzip'}
};
var req = http.request(options);
gunzip.on('data', function (buf) {
body += buf.toString();
});
gunzip.on('end', function() {
console.log(body);
// do whatever you want to do with body
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
gunzip.end();
});
req.pipe(gunzip);
More info on zlib library: http://nodejs.org/api/zlib.html
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 establish alexa voice downchannel stream in nodejs using https but i am getting parse error. here is the code
var https = require('https');
var fs = require('fs');
//read the accesstoken from text file.
var accessToken = fs.readFileSync('accessToken.txt','utf8');
var options = {
host: 'avs-alexa-na.amazon.com',
path: '/v20160207/directives',
scheme : 'https',
method: 'GET',
headers: {'Authorization': 'Bearer ${accessToken}'}
};
var req = https.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);
});
req.end();
when i am running the above code, i am getting error
problem with request: Parse Error
could you please help me where i am doing mistake.
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 am calling an http post request(login request from another 3rd party server) from node and want to save the cookie for future http requests.
var options = {
host: 'hostname.com',
port: '80',
path: '/login.php',
method: 'POST'
};
var data = qs.stringify({
uname: "validUsername",
password: "validPassword"
});
var req = http.request(options, function(response) {
console.log(response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
// console.log('Cookies: ' + response.getHeader("Set-Cookie"));
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write(data);
req.end();
How can i access and save the cookie from the response sent by the requested server?
You should be able to get the header with response.headers['set-cookie'];.
If you need to actually parse them, you could use a module like cookie (what Express uses) or cookiejar.
Guys I'm having trouble requesting to this URL.. It seems fine, but I always get the error ECONNRESET.
I wrote a little script in ruby and it worked fine. With cURL in the terminal also works.
I tried all the solutions on a lot of issues and stack overflow threads... Like these:
https://github.com/joyent/node/issues/5360
https://github.com/joyent/node/issues/5119
Any idea what it might be?
The url is: https://ecommerce.cielo.com.br/servicos/ecommwsec.do
var https = require('https');
var options = {
host: 'ecommerce.cielo.com.br',
path: '/servicos/ecommwsec.do',
//This is what changes the request to a POST request
method: 'GET',
};
https.globalAgent.options.secureProtocol = 'SSLv3_method';
callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(str);
});
}
var req = https.request(options, callback);
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
How are you ??
Let's try this solution.
app.js
Change your options:
var options = {
host: 'ecommerce.cielo.com.br',
path:'/servicos/ecommwsec.do',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(body),
'user-agent': 'node.js'
}
};
https.globalAgent.options.secureProtocol = 'SSLv3_method';
try{
var req = https.request(options, function(res)
{
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log("body: " + chunk);
}).on('end', function(cieloResponse){
console.log( cieloResponse);
});
console.log('Response:' + res);
});
req.end();
}catch(err){
console.log('ERRO: '+ err);
}