简体   繁体   中英

getting a parameter from a callback request using node.js and express

I'm using the twilio API and node.js to successfully record and transcribe calls. I'm trying to implement a callback when a call is recorded. According to the twilio API, I specify my callback URL:

twiml.record({
    transcribe: false,
    maxLength: 30, //seconds to record
    recordingStatusCallback: 'http://[url]:3000/recorded'
  })

Per the documentation , I should receive some parameters back via POST, so I am logging the request and response:

app.post('/recorded', (req, res) => {
  console.log(req, res)
})

What I get back in the log is a couple of giant objects, and I don't see anything that looks like the parameters I'm expecting. I'm guessing I'm supposed to be constructing that post function differently to get the parameters I need, but i'm not sure how, and I don't see any documentation in Twilio or online that shows how a successful callback function is structured.

Any ideas how to do this?

First of all add 'body-parser' module to your project.

npm install body-parser

And require it in the code:

var bodyParser = require('body-parser')

Now add it somewhere in the code before launching the server:

app.use(bodyParser.urlencoded({ extended: false }));  //this will add extracting of the body for every request to express server

After that, in your functions, where you are trying to find data, you can get it using such code:

app.post('/recorded', (req, res) => {
    console.log(req.body);  //body of the request in javascript object format
})

Find more info about 'body-parser' module here .

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