I'm trying to retrieve the body of a request via the response object.
var request = require('request');
request({
...
body: {
foo: 'bar'
}
}, function(err, res, body) {
var reqBody = res.request.body;
});
But the request body is now a Buffer. How can I turn this back into a JavaScript object?
Note: I can't store the request body in a variable with larger scope before making the http request.
Figured it out, way simpler than I thought.
var reqBody = res.request.body.toString();
reqBody = JSON.parse(reqBody);
First convert it to JSON, then convert the JSON to a JavaScript object.
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