Skip to content

Instantly share code, notes, and snippets.

@reflexdemon
Created August 11, 2020 15:00
Show Gist options
  • Save reflexdemon/62707a58f1b49753446178dcea8526ea to your computer and use it in GitHub Desktop.
Save reflexdemon/62707a58f1b49753446178dcea8526ea to your computer and use it in GitHub Desktop.

Print Request/Response for Node JS

This is a handy tool to print the node request library API response on console so that we can print it for debugging and also sending infomation to the API teams. I always try to print it out from the application to triage the issue with API teams.

function printResponse(response) {
  console.log(`Request:`);
  console.log(`${response.request.method} ${response.request.href}`)
  console.log(`Headers: ${JSON.stringify(response.request.headers)}`)
  console.log(`Response:`);
  console.log(`Headers: ${JSON.stringify(response.headers)}`)
  console.log(`Status Code: ${response.statusCode}`)
  console.log(JSON.stringify(response.body)) 
}

In the future this can turn into an npm project. Wait and see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment