Returns a JSON representation of response
npm i --save response-to-json
var http = require('http')
var resToJSON = require('response-to-json')
http.request({...}, function (res) {
// Default behavior
resToJSON(res)
/*
{
statusCode: 200
statusMessage: 'OK'
headers: {}
body: 'hello'
trailers: {}
method: 'GET'
url: 'http://localhost:80'
httpVersion: '1.1'
}
*/
// Pass true to include rawHeaders and rawTrailers
resToJSON(res, true) // true will return raw headers and trailers
/*
{
statusCode: 200
statusMessage: 'OK'
headers: {}
body: 'hello'
trailers: {}
method: 'GET'
url: 'http://localhost:80'
httpVersion: '1.1',
rawHeaders: [],
rawTrailers: []
}
*/
// Pass array of strings to include custom properties (supports keypaths)
res.customProp1 = 'hai'
resToJSON(res, ['customProp1']) // true will return raw headers and trailers
/*
{
statusCode: 200
statusMessage: 'OK'
headers: {}
body: 'hello'
trailers: {}
method: 'GET'
url: 'http://localhost:80'
httpVersion: '1.1',
customProp1: 'hai'
}
*/
})
MIT