Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request has incorrect content type when using Twilio and Google Cloud Functions

I am following this Twilio tutorial on how to reply to SMS messages with my app: https://www.twilio.com/docs/sms/tutorials/how-to-receive-and-reply-node-js

The tutorial assumes you're using Express, but I am doing this with a Cloud Function, so my code looks a bit different:

exports.sms = functions.https.onCall((req: any, res: any) => {
    const twiml = new MessagingResponse();

    if (req.body.Body === 'hello') {
        twiml.message('Hi!');
    } else if (req.body.Body === 'bye') {
        twiml.message('Goodbye');
    } else {
        twiml.message(
            'No Body param match, Twilio sends this in the request to your server.',
        );
    }

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

When I text my Twilio #, it hits that endpoint, but I get the following error:

Request has incorrect Content-Type. application/x-www-form-urlencoded

How do I get around this?

like image 825
Bryan Avatar asked May 17 '26 09:05

Bryan


1 Answers

It looks like you're mixing up callable type functions and normal HTTP type functions. Please read the documentation to understand the difference. Callable functions are intended to be invoked directly from your mobile app using the provided client SDK. They provide two arguments: an input data object, and a context. Callables do NOT provide "req" and "res". If you want control over the the response, you should be using a normal HTTP function with "onRequest" instead of "onCall".

like image 75
Doug Stevenson Avatar answered May 20 '26 01:05

Doug Stevenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!