Search code examples
node.jstwiliotwilio-apitwilio-twiml

How to create a valid TwilioML response object in node.js


I have a twilio webhook and I'm trying to structure the response with accordance with twiloML, I get an error response in twilio logs of

12200 The provided XML does not conform to the Twilio Markup XML schema. Please refer to the specific error and correct the problem.

const twilio =  require('twilio');

function defaultTwilioSuccess(){
        var response = new twilio.twiml.MessagingResponse();
        response.message('its alive');
        return response.toString();
    }


exports.handler = function(event, context, callback){
    ...
    .then(function() {
         return callback(null, {
                    "statusCode": 200,
                    "headers": {'Content-Type': 'text/xml'},
                    "body":   JSON.stringify(defaultTwilioSuccess())
                });

            });
  ...

Solution

  • the JSON.stringify , is not needed in this case since its the respone is an XML format

    return callback(null, {
                        "statusCode": 200,
                        "headers": {'Content-Type': 'text/xml'},
                        "body":  defaultTwilioSuccess()
                    });
    
                });