curl命令返回html而不是node.js



我正试图通过heroku创建并托管一个webhook,使用这个Facebook信使教程:https://developers.facebook.com/docs/messenger-platform/getting-started/webhook-setup.因此,我目前正在测试对本地主机的cURL请求。此请求:

curl -H "Content-Type: application/json" -X POST "localhost:3000/" -d "{""object"": ""page"", ""entry"": [{""messaging"": [{""message"": ""TEST_MESSAGE""}]}]}"

应在cmd中返回EVENT RECEIVED,并在第二个cmd窗口中返回TEST_MESSAGE,我正在执行应用程序。为了执行应用程序,我使用了$node index.js,我收到了"webhook侦听",这意味着我执行它的方式没有错误

这就是我的文件index.js的样子:

'use strict';
// Imports dependencies and set up http server
const
express = require('express'),
bodyParser = require('body-parser'),
app = express().use(bodyParser.json()); // creates express http server
// Sets server port and logs message on success
app.listen(process.env.PORT || 3000, () => console.log('webhook is listening'));
// Creates the endpoint for our webhook 
app.post('/webhook', (req, res) => {  
let body = req.body;
// Checks this is an event from a page subscription
if (body.object === 'page') {
// Iterates over each entry - there may be multiple if batched
body.entry.forEach(function(entry) {
// Gets the message. entry.messaging is an array, but 
// will only ever contain one message, so we get index 0
let webhook_event = entry.messaging[0];
console.log(webhook_event);
});
// Returns a '200 OK' response to all requests
res.status(200).send('EVENT_RECEIVED');
} else {
// Returns a '404 Not Found' if event is not from a page subscription
res.sendStatus(404);
}
});
// Adds support for GET requests to our webhook
app.get('/webhook', (req, res) => {
// Your verify token. Should be a random string.
let VERIFY_TOKEN = "somepassword"
// Parse the query params
let mode = req.query['hub.mode'];
let token = req.query['hub.verify_token'];
let challenge = req.query['hub.challenge'];
// Checks if a token and mode is in the query string of the request
if (mode && token) {
// Checks the mode and token sent is correct
if (mode === 'subscribe' && token === VERIFY_TOKEN) {
// Responds with the challenge token from the request
console.log('WEBHOOK_VERIFIED');
res.status(200).send(challenge);
} else {
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403);      
}
}
});

这是我得到的回应(一些html代码(:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /</pre>
</body>
</html>

我没有收到错误,只是收到了"错误"的返回

有人知道如何解决这个问题并接收"EVENT RECEIVED"one_answers"TEST_MESSAGE"吗?

注意:我看到了以下文章,Curl命令不返回,Prometheus API返回HTML而不是JSON,我确实更改了教程中与Facebook信使机器人错误有关的一些代码意外令牌';JSON中的位置0,但我仍然无法修复此问题。

我还阅读了如何从终端/命令行用Curl发布JSON数据以测试Spring REST?,但它也没有解决我的问题。

您忘记了在curl命令中使用的url中包含/webhook

curl -H "Content-Type: application/json" -X POST "localhost:3000/webhook"

最新更新