使用 Twilio 和 Google Cloud Functions 时请求的内容类型不正确

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

我正在关注有关如何使用我的应用回复短信的 Twilio 教程: https://www.twilio.com/docs/sms/tutorials/how-to-receive-and-reply-node-js

本教程假设您使用的是 Express,但我使用的是 Cloud Function,因此我的代码看起来有点不同:

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());
});

当我给我的 Twilio # 发短信时,它到达了那个端点,但我收到以下错误:

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

我该如何解决这个问题?

你好像搞错了 callable type functions and normal HTTP type functions。请阅读文档以了解差异。可调用函数旨在使用提供的客户端 SDK 从您的移动应用程序直接调用。它们提供两个参数:输入数据对象和上下文。 Callables 不提供 "req" 和 "res"。如果你想控制响应,你应该使用 "onRequest" 而不是 "onCall".

的普通 HTTP 函数