Skip to content Skip to sidebar Skip to footer

How To Get A Twilio Sms Body Text In Node.js

I'm working on a side-project of mine which integrates with Twilio. It's a Node.js app running on the Hapi.js framework. I have the app set up with Twilio so that when I text my Tw

Solution 1:

With Hapi, if you want to read all the request body into memory before running your handler you need to set the payload config:

server.route({
    config: {
        payload: {
            output: 'data'
        }
    },
    method: 'POST',
    path: '/textMessage',
    handler: function(request, reply) {
        console.log('Body was', request.payload.toString()); 
        reply('...');
    }
});

From the Hapi API docs:

'data' - the incoming payload is read fully into memory. If parse is true, the payload is parsed (JSON, form-decoded, multipart) based on the 'Content-Type' header. If parse is false, the raw Buffer is returned. This is the default value except when a proxy handler is used.

More options here: http://hapijs.com/api#route-options

Solution 2:

After trying different methods to retrieve the request payload from Twilio, I found that the Twilio request body can be accessed via request.url.query, and therefore, the Body of the text, can be retrieved via request.url.query.Body. Thanks for the help @MattHarrison!

Post a Comment for "How To Get A Twilio Sms Body Text In Node.js"