Home > Software engineering >  Req.body of a twilio webhook request is undefined node js
Req.body of a twilio webhook request is undefined node js

Time:10-03

I tried following this twilio article:

https://www.twilio.com/blog/parsing-an-incoming-twilio-sms-webhook-with-node-js

The following is my server in its entirety

// Imports
const http = require('http')
const express = require('express')
const app = express()
const port = 80
const MessagingResponse = require('twilio').twiml.MessagingResponse;
// Static Files
app.use(express.static('public'))

// Set views
app.set('views', './views')

app.post('/sms', (req, res) => {
    const twiml = new MessagingResponse();

    // Access the message body and the number it was sent from.
    console.log(req.IncomingMessage.body)


    res.writeHead(200, {'Content-Type': 'text/xml'});
    res.end(twiml.toString());
});

// Listen on port
app.listen(port, () => {
    console.log(`Running on port ${port}`)
})

I have the webhook of my twilio phone posting to "http://{domain}.com/sms"

Every time I send a text to my twilio phone, I get the following error:

TypeError: Cannot read property 'body' of undefined
    at app.post (/home/ubuntu/{domain}/server.js:24:37)
    at Layer.handle [as handle_request] (/home/ubuntu/{domain}/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/ubuntu/{domain}/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/ubuntu/{domain}/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/ubuntu/{domain}/node_modules/express/lib/router/layer.js:95:5)
    at /home/ubuntu/{domain/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/ubuntu/{domain}/node_modules/express/lib/router/index.js:335:12)
    at next (/home/ubuntu/{domain}/node_modules/express/lib/router/index.js:275:10)
    at serveStatic (/home/ubuntu/{domain}/node_modules/serve-static/index.js:75:16)
    at Layer.handle [as handle_request] (/home/ubuntu/{domain}/node_modules/express/lib/router/layer.js:95:5)

CodePudding user response:

I don't think the req object has any property called IncomingMessage. Instead you could try:

console.log(`Incoming message from ${req.body.From}: ${req.body.Body}`);

So based on the twilio specs, the endpoint would look like:

app.post('/sms', (req, res) => {
  const twiml = new MessagingResponse();

  // Access the message body and the number it was sent from.
  console.log(`Incoming message from ${req.body.From}: ${req.body.Body}`);

  res.writeHead(200, {'Content-Type': 'text/xml'});
  res.end(twiml.toString());
});

Since the message body is in the req.body.Body parameter, so console logging this property will show you the message text.

CodePudding user response:

I'm not sure if this is correct but when I'm using express I use req.body.IncomingMessage

console.log(req.body.IncomingMessage)

If doesn't work please don't down vote. Just comment and I'll delete answer

  • Related