Newly Practicing Mortician
Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

response-to-json

1.0.0 • Public • Published

response-to-json Build Status

Returns a JSON representation of response

Installation

npm i --save response-to-json

Usage

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'
  }
  */
})

License

MIT

install

npm i response-to-json

Downloadsweekly downloads

1

version

1.0.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability