简体   繁体   中英

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)
})

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM