【问题标题】:Twilio Receive Fax, Body is EmptyTwilio 接收传真,正文为空
【发布时间】:2020-06-02 00:22:50
【问题描述】:

我正在尝试开始使用 Twilio 的可编程传真 API,并且我已经完成了他们的入门指南。但是,当我收到传真时,我会将请求正文记录到控制台。然而,身体只是一个空的对象。 我不确定出了什么问题。

const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();

// Parse any incoming POST parameters
app.use(bodyParser.json({ extended: false }));

// Define a handler for when the fax is initially sent
app.post('/fax/sent', (req, res) => {
  // Let's manually build some TwiML. We can choose to receive the
  // fax with <Receive>, or reject with <Reject>.
  console.log(req.body);

  const twiml = `
  <Response>
    <Receive action="/fax/received" mediaType="application/pdf" storeMedia="true"/>
  </Response>
  `;

  // Send Fax twiml response
  res.type('text/xml');
  res.send(twiml);
});

// Define a handler for when the fax is finished sending to us - if successful,
// We will have a URL to the contents of the fax at this point
app.post('/fax/received', (req, res) => {
  // log the URL of the PDF received in the fax
  console.log(req.body);    
  // Respond with empty 200/OK to Twilio
  res.status(200);
  res.send(req.body);
});

// Start the web server
http.createServer(app).listen(3000, () => {
  console.log('Express server listening on port 3000');
});

这是我在控制台中返回的内容。您可以看到记录的空对象...

Express server listening on port 3000
{}

更新: 我将正文解析器中间件更改为使用 urlencoded app.use(bodyParser.urlencoded({extended: false })); 我得到了对象,但我没有看到媒体网址......

【问题讨论】:

    标签: javascript node.js express twilio


    【解决方案1】:

    使用更高版本的 Express, 4.16.0 - Release date: 2017-09-28,您不需要 body-parser。

    // Body Parser Middleware
    app.use(express.json());
    app.use(express.urlencoded({ extended: false }));
    

    【讨论】:

      【解决方案2】:

      BodyParser 自其文档编写以来已更新。你需要做的

      app.use(bodyParser.urlencoded({ extended: false }));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-10
        • 1970-01-01
        • 1970-01-01
        • 2012-06-20
        • 1970-01-01
        • 1970-01-01
        • 2021-10-15
        相关资源
        最近更新 更多