2016-07-31 34 views
0

我使用Typeform webhook feature來生成JSON結果,只要有人在嵌入式調查中點擊提交按鈕,我就會像文檔中描述的那樣使用服務像RequestBin,但是當我使用ngrok通過命令從Typeforms webhook解析原始主體的問題

ngrok http 3000 

暴露我的本地應用程序,然後設置爲網絡掛接目標URL的基本路線,我得到滿意的複印件。這裏是快速的路線:

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) 

當我打使用郵差在原車身的唯一輸出途徑,有趣的是是:

{"title": "Test"} 

這不是由我在上面發佈的Express路由中的console.log語句驗證的。

爲什麼我會通過RequestBin接收有用的數據,但不會在我的本地應用程序的實際服務器端接收它?

回答

2

看起來你錯誤地使用了請求(在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應該包含在提交的結果。讓我知道如果它仍然不起作用!