提问者:小点点

webhooks的NodeJS侦听器返回空主体


我已经在这个上了大约2天,准备把我的头发拔出来。

我有一个系统,激发简单的网络钩子,我需要捕获和解析。我已经创建了一个非常简单的Nodejs侦听器,并且正在运行Ngrok来创建公共地址。我收到了一个请求,但不管我做什么,身体都是空的。无论content-type或App.Use(BodyParser.URLENCoded())。

主体仍然返回为}

'use strict'

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

// Create a new instance of express
const app = express()

// Tell express to use the body-parser middleware and to not parse extended bodies
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

// Route that receives a POST request to /sms
app.post('/', function (req, res) {
    console.log(req);
  const body = req.req
  console.log("body")
  console.log(body);
  res.set('Content-Type', 'text/plain')
  res.send(`You sent: ${body} to Express`)
})


// Tell our app to listen on port 4545
app.listen(4545, function (err) {
  if (err) {
    throw err
  }

  console.log('Server started on port 4545')
})

但是,如果我将地址设置为https://webhook.site或https://pipedream.com,那么主体确实已经发布了。我尝试过content-type,urlencode的组合,甚至认为它是gzip之类的,甚至部署在Azure VM和其他一些程序上。什么都没有。

但当我将地址设置为webhooks或pipedream时,数据显示没有问题。

有人能让我走上正确的道路来解决这个问题吗?

非常感谢。


共1个答案

匿名用户

您可以在中获取主体数据,包括主体解析器中间件,该中间件解析主体并设置属性。

var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: false })) 

// Route that receives a POST request to /sms
app.post('/', function (req, res) {
  console.log(req);
  const body = req.body
  console.log("body")
  console.log(body);
  res.set('Content-Type', 'text/plain')
  res.send(`You sent: ${body} to Express`)
})