从Typeforms webhook解析原始正文时出现问题

Problems parsing raw body from Typeforms webhook

本文关键字:问题 正文 原始 Typeforms webhook      更新时间:2023-09-26

每当有人点击嵌入式调查上的提交按钮时,我都会使用Typeform webhook功能将结果生成为JSON,当我使用RequestBin这样的服务时,我得到的结果与文档描述的完全一样,但当我通过命令使用ngrok公开我的本地应用程序时

ngrok http 3000

然后将底层路由设置为webhook目标URL,我得到了不满意的输出。这是Express的路线:

app.post('/receiveWebhook', function(req, res){
    console.log(req);
    console.log(req.title);
    res.send(200);
});

产生服务器端输出:

IncomingMessage {
   _readableState: 
       ReadableState {
           objectMode: false,
           highWaterMark: 16384,
           buffer: [],
           length: 0,
           pipes: null,
           pipesCount: 0,
           flowing: null,
           ended: false,
           endEmitted: false,
           reading: false,
           sync: true,
           needReadable: false,
           emittedReadable: false,
           ....
      body: {},
      params: {},
      ... 
      (can post the entire contents on Dropbox if comments think it is necessary)

当我使用Postman到达路线时,有趣的是,Raw Body中唯一的输出是:

{"title": "Test"}

我在上面发布的Express路由中的console.log语句没有验证这一点。

有什么想法吗?为什么我会通过RequestBin接收有用的数据,但不会在本地应用程序的实际服务器端接收?

您似乎(在express中)错误地使用了请求。Typeform webhook将结果作为请求的主体,因此您需要使用body-parser来获得正确的数据。

看看这里:https://github.com/TypeformIO/SimpleLiveReports/blob/master/index.js

重要位:

包括正文分析器

var bodyParser = require('body-parser')

将其用作中间件

app.use(bodyParser.json());

使用req.body使用数据,例如:

app.post('/receive_results', function handleReceiveResults(req, res) { console.log('Got results!'); var body = req.body; saveAnswers(body.token, body.answers, body.uid); res.send('Ok!'); });

使用此设置,req.body应该包含提交的结果。如果它仍然不起作用,请告诉我!