I'm kind of lost. Is it possible to get the response body as a string in .on(response)
(see below)? Or am I crazy? I know I can .pipe
the response to a WriteStream
, but I need both the response body and the response statusCode at the same place in order to continue my code. Thanks!
var request = require("request");
request(url)
.on('response', function(response) {
console.log(response.statusCode); // 200
console.log(/* I need to get response body string here */); // <--- can I have the response body as a string here?
})
.on("error", function(err){
console.log("Problem reaching URL: ", err);
});
I would make in a comment, but I can't.
As in the npm page (considering you don't need pipe), you can have the body along with the response in: :
var request = require("request");
request(url, function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With