Twilio 接收传真,Body 为空

Twilio Receive Fax, Body is Empty

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

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

这是我在控制台中得到的结果。您可以看到记录的空 object...

Express server listening on port 3000
{}

更新: 我将 body 解析器中间件更改为使用 urlencoded app.use(bodyParser.urlencoded({ extended: false })); 我得到 object 但我没有看到媒体 url...

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

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

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

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