bannerboy
bannerboy

Reputation: 43

how to serve Twiml Nodejs

I am having a hard time create my TWIML files with nodejs. I am creating outbound calls and they work with a static XML file or twiml bin but not my endpoint.

do you know what is wrong?

app.post('/twiml-generator', function(req, res){
    var name = "billy";
    //Create TwiML response
    var twiml = new twilio.TwimlResponse();

    twiml.say("Hello from your pals at Twilio! Have fun. Love " + name);

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

});

then when i go to initiate the call

  client.calls.create({ 
    url: 'http://myHOSTEDsite.com/twiml-generator',//ISSUE HERE but if i use a twiml bin or static xml, it works// so my endpoint must be the issue 
    to: targetNumber, 
    from: "+14444444444", // my trail number 
    timeout: 12

  }, function(err, call) { 
    console.log("call made"); 
    //console.log(call)

  }); 

Upvotes: 0

Views: 457

Answers (2)

Ilya Umanets
Ilya Umanets

Reputation: 73

This is what Twilio doc says

response.type('text/xml');
response.send(twiml.toString());

Upvotes: 0

Alex Baban
Alex Baban

Reputation: 11732

Instead of

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

try

res.set('Content-Type', 'text/xml');
res.send(twiml.toString());

Upvotes: 1

Related Questions