Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix an invalid content-type header in Twilio using node.js?

I've been working on the Twilio projects from Radical Skills, and I've been having trouble responding to incoming text messages using JavaScript. I'm able to use TwiML, which is XML, but I am unable to respond in JavaScript like the example shows.

My message request URL in Twilio is /incoming/handle-incoming-text.xml.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Redirect method="GET">incoming/sms.js</Redirect>
</Response>

When my Twilio application receives a text message, it redirects to sms.js. I've copied this code directly from the example.

var twilio = require('twilio');
var resp = new twilio.TwimlResponse();

if( req.Body == 'hello'){
    resp.message('Hi!');
}else if( req.Body == 'bye'){
    resp.message('Goodbye');
}
console.log(resp.toString());

When I send a text message saying "hello" from my phone to my Twilio number, I get the following responses.

GET /incoming/handle-incoming-text.xml 200 1ms - 240b
GET /incoming/sms.js 200 39ms - 251b

However, I do not get a text back from my Twilio number saying "Hi!" In fact, I do not get a response at all.

When I checked the error logs, it gave me a 12300 Invalid Content-Type error with the following message.

Twilio attempted to retrieve content from the URL but was unable to process the application/javascript Content-Type header in the response. Please see the Twilio XML Markup Documentation for more details on valid Content-Type settings.

One of the headers in my response is "Content-Type application/javascript." According to the twilio-node helper library documentation, it should output XML. However, it does not appear to be working properly.

I've spent several hours trying to figure this out, and I still haven't been able to make it work.

like image 731
BillFienberg Avatar asked Feb 12 '15 06:02

BillFienberg


People also ask

How do you change the header and Content-Type in node JS?

use(function(req, res, next) { req. header("Content-Type", "application/json"); res. header("Content-Type", "application/json"); next(); });

What is Content-Type header in node JS?

Content-Type in the response is the header we can set to inform how client would interpret the data from the server. For example, if you are sending down an HTML file to the client, you should set the Content-Type to text/html, which you can with the following code: response. setHeader("Content-Type", "text/html");


1 Answers

Twilio developer evangelist here.

It seems that the Radical Skills project is missing some detail. I'd suggest you take a read through this blog post on the node.js helper written by my colleague Kevin. It takes you through how to set up a web server to properly respond to Twilio. For this particular issue, you can take a look at the section on handling SMS.

Hope this helps, let me know if I can help more.

like image 91
philnash Avatar answered Oct 03 '22 09:10

philnash