Search This Blog

Monday, April 23, 2018

nodejs gzip response on chunks are junk

If you are trying to call a request and trying to parse the response and getting

junk characters on the buffer concat of chunks, do this.

Change the header accept encoding.

"accept-encoding": "br" // remove gzip if present


You can check the encoding of response sent via server using this.

console.log('server encoded the data as: ' + (res.headers['content-encoding'] || 'identity'))


once you know what kind of encoding, remove it from the header and fire request again.

instead of pushing chunks to the array you can directly concat the string to the body.

body+=chunk.toString();

// Sample code


sample post code on server.js (node)


app.post('/app/call, function(req,res){

var http = require("https");

var response;

var options = {

"method": "POST",

"hostname": "xx",

"port": null,

"path": "/v3/topics",

"gzip":"true",

"headers": {

"origin": "https://xxi.com",

"accept-encoding": "br",

"accept-language": "en-US,en;q=0.8",

"authorization": "Bearer 7f850f07-xxxxxx",

"content-type": "application/json",

"accept": "*/*",

"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36",

"referer": https://sxxxx,

"authority": "xxxxx",

"cache-control": "no-cache",


}

};

var req = http.request(options, function (data) {

var chunks = [];

var body;

data.on("data", function (chunk) {

//console.log('server encoded the data as: ' + (res.headers['content-encoding'] || 'identity'))

chunks.push(chunk);

body+=chunk.toString();

});

data.on("end", function () {

// body = Buffer.concat(chunks);

console.log(body.toString());

response = body.toString();

res.write(response);

res.end();

});

});


req.end();

});


No comments:

Post a Comment