从Typeforms webhook解析原始主体时出现问题

时间:2016-07-31 16:36:10

标签: javascript express post webhooks body-parser

每当有人点击嵌入式调查中的提交按钮时,我都会使用Typeform webhook feature为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接收有用数据而不是在本地应用的实际服务器端接收它的想法?

1 个答案:

答案 0 :(得分:3)

好像你错误地使用了这个请求(在快递中)。 Typeform webhook将结果作为请求的主体,因此您需要使用body-parser来获取正确的数据。

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

重点:

包括body-parser

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应包含提交结果。如果它仍然无效,请告诉我!