4

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)
})
mrsB2013
  • 171
  • 2
  • 8

2 Answers2

3

Simplest way to do this is to start a netcat server on any port:

$ nc -l -p 8080

and change the URL to localhost:

https://localhost:8080/v1beta1/text:synthesize?key=API_KEY

Obviously, you won't be able to see the response, but the entire raw request data will be available for you to inspect in the terminal you have netcat running

Swayam
  • 580
  • 6
  • 11
-1

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.

robertklep
  • 198,204
  • 35
  • 394
  • 381
  • 3
    This is incorrect. *require('request').debug = true* does not produce raw http requests, but just some pretty-printing. – El Sampsa Jan 02 '19 at 11:27
  • @ElSampsa I'm showing all documented debugging options. The latter (number 3) explains that it can be used to _"view request and response headers and bodies"_. – robertklep Jan 02 '19 at 12:27