【问题标题】:Empty params on Twiml POST requestTwiml POST 请求上的空参数
【发布时间】:2016-01-28 21:06:42
【问题描述】:

一点背景。我有一个正在运行的网络应用程序正在尝试接收来自 Twilio 的传入文本。我已将 Twilio 的 sms twiml url 配置为指向我的应用程序中的路由:

http://my-app-name.com/api/twiml

在发出请求时,我执行了一些代码:

if (Meteor.server) {
    // use picker to make a server side route
    Picker.route('/api/twiml', (params, req, res, next) => {
        console.log(util.inspect(params, {showHidden: true, colors: true}));
        console.log(util.inspect(req.body, {showHidden: true, colors: true}));

        // message data (not populating?)
        let messageSid = params.query.MessageSid,
            accountSid = params.query.AccountSid,
            from = params.query.From,
            to = params.query.To,
            body = params.query.Body;

        //console.log(messageSid + '\n' + accountSid + '\n' + from + '\n' + to + '\n' + body)

        //from = '+' + from.trim();
        //to = '+' + to.trim();

        let responseBody = 'Thanks';

        res.writeHeader(200, {'Content-Type': 'application/xml'});

        return '<?xml version="1.0" encoding="UTF-8"?><Response><Sms from="[TWILIOFROM]" to="[TO]">[BODY]</Sms></Response>'
            .replace('[TWILIOFROM]', myNumber)
            .replace('[TO]', from)
            .replace('[BODY]', responseBody);
    });
}

当我发送我的 twilio 号码时,代码会运行,但我的 Twilio 日志中出现 11200 HTTP retrieval failure 错误。我的应用程序的日志正在输出第一个 console.log,但我没有收到来自 params.query 的任何数据。

{ query: {} }

第二个console.log:console.log(util.inspect(req.body, {showHidden: true, colors: true}));吐出一些垃圾:[90mundefined[39m

是否应该放弃参数,尝试解析请求体?

我对 REST api 很陌生,所以我确信我缺少一些非常基本的东西。

【问题讨论】:

    标签: node.js rest meteor twilio twilio-twiml


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

    当 Twilio POST 到您配置的 SMS url 时,parameters are sent as the body of the request,而不是查询字符串参数。

    我对 Picker 不太熟悉,但是 documentation suggests you can use Express middleware,包括 body-parser。如果您将 Picker 与 body-parser 连接起来,那么您应该能够从 req.body 获取参数。这样的事情可能会起作用(注意第 2 行和第 3 行,包括 body-parser):

    if (Meteor.server) {
        var bodyParser = Meteor.npmRequire( 'body-parser');
        Picker.middleware( bodyParser.urlencoded( { extended: false } ) );
    
        // use picker to make a server side route
        Picker.route('/api/twiml', (params, req, res, next) => {
            console.log(util.inspect(params, {showHidden: true, colors: true}));
            console.log(util.inspect(req.body, {showHidden: true, colors: true}));
    
            // message data (not populating?)
            let messageSid = req.body.MessageSid,
                accountSid = req.body.AccountSid,
                from = req.body.From,
                to = req.body.To,
                body = req.body.Body;
    
            //console.log(messageSid + '\n' + accountSid + '\n' + from + '\n' + to + '\n' + body)
    
            //from = '+' + from.trim();
            //to = '+' + to.trim();
    
            let responseBody = 'Thanks';
    
            res.writeHeader(200, {'Content-Type': 'application/xml'});
    
            let twiml =  '<?xml version="1.0" encoding="UTF-8"?><Response><Sms from="[TWILIOFROM]" to="[TO]">[BODY]</Sms></Response>'
                .replace('[TWILIOFROM]', myNumber)
                .replace('[TO]', from)
                .replace('[BODY]', responseBody);
    
            res.end(twiml);
        });
    }
    

    让我知道这是否有帮助。

    编辑:我认为,您实际上需要使用res.send 将其发送到响应对象,而不是返回 TwiML。我已经更新了上面的代码。

    【讨论】:

    • 这完全有帮助,非常感谢!我仍然收到11200 HTTP retrieval failure。我猜我的回答不正确?
    • 那么,您现在看到请求正文中的传入参数了吗?
    • 是的,我可以看到 req.body 参数。
    • 我认为您需要res.send(twiml) 而不是返回它。查看我的编辑。
    • 轰隆隆!太棒了,它正在工作。感觉我们一起度过了难关。祝应用程序的其余部分好运!
    猜你喜欢
    • 2023-03-27
    • 2020-06-26
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 2018-06-16
    相关资源
    最近更新 更多