How do I convert result from opening gzip file to string - node.js

var request = require('request')
request(
{ method: 'GET'
, uri: 'http://www.examplewebsite.com'
, gzip: true
}
, function (error, response, body) {
console.log(body); //I am trying print body here
console.log(response.body); // //I am trying print body here too
}
)
All I received is just �V*.I,)-V�*)*M�QJI,IT��V��,.���%�E��)JV�����d��$1
If I try to use JSON.stringify(body), the result is:
\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0003�V*.I,)-V�*)*M�QJI,IT��V��,.\u0001��\u0019�\u0005%�E��)JV�����\u0000d��$1\u0000\u0000\u0000
All I want is to see the plain string. How do I do that?

I would expect that setting gzip : true would decompress the response body automatically, but perhaps the server doesn't set the correct Content-Encoding header.
In that case, you can try this:
const zlib = require('zlib');
const request = require('request');
request({
method : 'GET',
uri : 'http://www.examplewebsite.com',
gzip : true,
encoding : null,
}, function (error, response, body) {
let decompressed = zlib.gunzipSync(body).toString();
...
})

Related

Using request module and passing the body to the next request

I am using request to get an image:
request(req.body.imageUrl, {encoding: null}, function(error, response, body) {
I then want to use the body to pass to an api that uses a multipart form and send that image (which is now in the body). I don't want to write the file to disk and then readstream from the disk again. I basically want to enter the formData of the next request by using the Buffer of the body, but it is not working.
So, for the options in the next request I have:
const options = {
method: "POST",
url: coreURL,
headers: {
"Content-Type": "multipart/form-data"
},
formData : {
file : new Buffer.from(body,'binary')
}
};
And this does not work, if I write the body to a file fs.writeFileSync(fileName, body, 'binary');
And then read in the options formData : { file : fs.createReadStream(fileName)}
it works but I cannot use the disk, so need an alternative way to pass the body into the next post as multipart form data.
Any ideas?
POC:
let request = require('request');
request.post({
url: "http://httpbin.org/anything",
formData: {
myfile: request.get("https://www.gravatar.com/avatar/f056d36f9e273fd4740ec8a5cea1348a"),
}
},
function (err, req, body) {
console.log(body);
}
);

Binary file to base64 nodejs

I'm getting a binary audio file when I call the api tts.speech.microsoft.com and I would like to transform this binary into a base64 string.
I've been trying a lot of things, for example:
Buffer.from(body, "binary").toString("base64");
does not work.
I'm not sure 'binary' is the exact word, but it's not a readable format.
Thank you for your help.
I guess you were following the section Make a request and save the response of the offical document Quickstart: Convert text-to-speech using Node.js to write your code, as below.
var request = require('request');
let options = {
method: 'POST',
baseUrl: 'https://westus.tts.speech.microsoft.com/',
url: 'cognitiveservices/v1',
headers: {
'Authorization': 'Bearer ' + accessToken,
'cache-control': 'no-cache',
'User-Agent': 'YOUR_RESOURCE_NAME',
'X-Microsoft-OutputFormat': 'riff-24khz-16bit-mono-pcm',
'Content-Type': 'application/ssml+xml'
},
body: body
};
function convertText(error, response, body){
if (!error && response.statusCode == 200) {
console.log("Converting text-to-speech. Please hold...\n")
}
else {
throw new Error(error);
}
console.log("Your file is ready.\n")
}
// Pipe the response to file.
request(options, convertText).pipe(fs.createWriteStream('sample.wav'));
So I change the offical code above to create a function encodeWithBase64 to encode body with Base64.
function encodeWithBase64(error, response, body){
if (!error && response.statusCode == 200) {
var strBase64 = Buffer.from(body).toString('base64');
console.log(strBase64);
}
else {
throw new Error(error);
}
console.log("Your file is encoded with Base64.\n")
}
// Pipe the response to file.
request(options, convertText);
Or you can use the npm packages base64-stream and get-stream to get the string with Base64 from body.
var base64 = require('base64-stream');
const getStream = require('get-stream');
(async () => {
var encoder = new base64.Base64Encode();
var b64s = request(options).pipe(encoder);
var strBase64 = await getStream(b64s);
console.log(strBase64);
})();
Otherwise, stream-string also can do it.
var base64 = require('base64-stream');
const ss = require('stream-string');
var encoder = new base64.Base64Encode();
var b64s = request(options).pipe(encoder);
ss(b64s).then(data => {
console.log(data);
})

Request module return unicode characters

I'm trying to get normal response using request module in node.js
and i've got problem to get response from amazon.com as normal string
i dont know why, but ive got problem only with amazon.com (eg. amazon.it, amazon.co.uk does return normal string).
const request = require('request');
request.get(
{
uri: 'https://www.amazon.com',
encoding: 'utf-8'
},
function (error, response, body) {
console.log(body)
});
The code above return something like:
b��╝��W>�S�Uk��z�=8~r����9|r|P^?}p o╗��l���ߋ�t`ޜ^]��n!��
���U�>>�#�w-z�.��O�����Oo��������y�����g�N�/��{����_>���鳟�=s���w?�z��_W)i؃�
��;���2��9<�0ٷ8����<=�ϱ��ղ��3�=(�"�ԯ�; �3��=�8�2;=���28����#+,3��0"�+DZ �)�2�<�
���7�(W?�8�9\?�)#'���";�ķ���ܣ�ѽ����|�8 ��╚ ��'
The response returned by Amazon is gzipped. You have to provide the gzip option to your request.
const request = require('request');
request.get(
{
uri: 'https://www.amazon.com',
gzip: true,
},
function (error, response, body) {
console.log(body)
});

Send unparsed data in a Node JS request

I want to send a POST request (for example, with the 'request' module), but I don't find a way of sending unparsed data*.
*unparsed data => copied directly from the Chrome dev tool. Something like: tipo_accion=3&filtro=descarga&fecha=actual
It would also do the trick some way of translating that string to JSON.
I've tried so far with...
var request = require('request');
request.post({ url: 'https://target.com/form/', form: 'tipo_accion=3&filtro=descarga&fecha=actual' },
function (error, response, body) {
console.log(body)
}
);
... but it didn't work.
Firstly you should understand the difference between the request methods post and get.
The structure that you want to send:
tipo_accion=3&filtro=descarga&fecha=actual is telling me that you want to use a get request. So the proper code for that will be something like:
request(
'https://target.com/form/&tipo_accion=3&filtro=descarga&fecha=actual',
function (error, response, body) {
console.log(body)
},
);
But if it is a post request then you should use the json format
request.post({
url: 'https://target.com/form/', form: {
tipo_accion: 3,
filtro: 'descarga',
fecha: 'actual'
},
function(error, response, body) {
console.log(body)
}
);
You can convert form_data to string format and it works for me you can try it :
const request = require('request-promise');
var j = request.jar()
var data_fom = `a=value_a&b=value_b&c=value_c`
request({
url:"https://target.com/form/",
jar:j,
method:"POST",
resolveWithFullResponse: true,
form:data_form
})

Encoding issue with requesting JSON from StackOverflow API

I can't figure this out for the life of me. Below is an implementation with the request module, but I've also tried with the node-XMLHttpRequest module to no avail.
var request = require('request');
var url = 'http://api.stackexchange.com/2.1/questions?pagesize=100&fromdate=1356998400&todate=1359676800&order=desc&min=0&sort=votes&tagged=javascript&site=stackoverflow';
request.get({ url: url }, function(error, response, body) {
if (error || response.statusCode !== 200) {
console.log('There was a problem with the request');
return;
}
console.log(body); // outputs gibberish characters like �
console.log(body.toString()); // also outputs gibberish
});
Seems to be an encoding issue, but I've used the exact same code (with native XHR objects) in the browser and it works without problems. What am I doing wrong?
The content is gzipped. You can use request and zlib to decompress a streamed response from the API:
var request = require('request')
,zlib = require('zlib');
var url = 'http://api.stackexchange.com/2.1/questions?pagesize=100&fromdate=1356998400&todate=1359676800&order=desc&min=0&sort=votes&tagged=javascript&site=stackoverflow';
request({ url: url, headers: {'accept-encoding': 'gzip'}})
.pipe(zlib.createGunzip())
.pipe(process.stdout); // not gibberish
Further Reading: Easy HTTP requests with gzip/deflate compression
While pero's answer is correct, there's a simpler way to do this.
Since you're using request, you can also just add the gzip: true flag:
var request = require('request');
var url = 'http://api.stackexchange.com/2.1/questions?pagesize=100&fromdate=1356998400&todate=1359676800&order=desc&min=0&sort=votes&tagged=javascript&site=stackoverflow';
request.get({ url: url, headers: {'accept-encoding': 'gzip'}, gzip: true }, function(error, response, body) {
console.log(body); // not gibberish
});

Resources