我正在尝试在Node.JS中创建一个接收POST请求的应用。此刻,我让它们通过ngrok隧道传输,我可以毫无问题地获得标头,但无法获得请求正文。这是我的代码:
const http = require("http");
const crypto = require("crypto");
const qs = require("qs");
const bodyParser = require("body-parser");
const slackSigningSecret = process.env.SLACK_SIGNING_SECRET;
http.createServer(function(request) {
let x_slack_signature = request.headers["x-slack-signature"]; // Get the Slack signature
let raw_body = request.body; // Get the request body
let x_slack_timestamp = request.headers["x-slack-request-timestamp"]; // Get the timestamp of the request
(...)
}).listen(3000);
但是,当我使用定义的变量创建控制台日志时,raw_body
返回空值或未定义。
自从我阅读似乎很有效以来,我就一直尝试qs.stringify
。还带有正文解析器;我收到此错误body-parser deprecated undefined extended: provide extended option
。如果我编写扩展选项,则输出以下内容:
function urlencodedParser (req, res, next) {
if (req._body) {
debug('body already parsed')
next()
return
}
req.body = req.body || {}
// skip requests without bodies
if (!typeis.hasBody(req)) {
debug('skip empty body')
next()
return
}
debug('content-type %j', req.headers['content-type'])
// determine if request should be parsed
if (!shouldParse(req)) {
debug('skip parsing')
next()
return
}
// assert charset
var charset = getCharset(req) || 'utf-8'
if (charset !== 'utf-8') {
debug('invalid charset')
next(createError(415, 'unsupported charset "' + charset.toUpperCase() + '"', {
charset: charset,
type: 'charset.unsupported'
}))
return
}
// read
read(req, res, next, parse, debug, {
debug: debug,
encoding: charset,
inflate: inflate,
limit: limit,
verify: verify
})
}
我在做什么错?
我试图先获得完整的身体,然后再将其分成一些类似的数组
预先感谢