Search code examples
node.js

Node.js request - print entire http request (raw) of a post


I am using the request library in Node.js to Google's text-to-speech API. I would like to print out the request that is being sent like in this python example.

Here is my code:

const request = require('request');


const headers = {headers: {'input': {'text':'I want to say this'}, 'voice':{  'languageCode' : 'en-US'},'audioConfig':{'audioEncoding': 'MP3'}}}

request.post('https://texttospeech.googleapis.com/v1beta1/text:synthesize?key=API_KEY',headers, (error, res, body) => {
  if (error) {
    console.error(error)
    return
  }
  console.log(`statusCode: ${res.statusCode}`)
  console.log(body)
})

Solution

  • This is documented here:

    There are at least three ways to debug the operation of request:

    1. Launch the node process like NODE_DEBUG=request node script.js (lib,request,otherlib works too).

    2. Set require('request').debug = true at any time (this does the same thing as #1).

    3. Use the request-debug module to view request and response headers and bodies.