aws sns 确认订阅请求处理问题

aws sns confirm subscription request processing issue

我正在尝试为 s3 中的存储桶实施 aws sns 服务,我正在关注此文档 https://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.html 根据这一点,在确认订阅的请求中将有订阅 url,这将到达我们提供的 url,但我在请求中收到空 body。 我试图记录 body 但给了我一个空的 object。并尝试使用 body 解析器但结果相同。

这是我正在实施的路线。

 router.post("/s3FileCallback", function (req, res) {
      debugger;
      var bodyParser = require('body-parser');
      var app = express();
      app.use(bodyParser.urlencoded({ extended: false }));
      app.use(bodyParser.json())
      if (req.get("x-amz-sns-message-type") == "SubscriptionConfirmation") {
        console.log("arn" + req.get("x-amz-sns-topic-arn"));
        const subscribeUrl = req.body.SubscribeURL;
        console.log("subscribeUrl" + subscribeUrl);
})

有没有我遗漏的东西。谁能给我指出正确的方向。

我找到了我丢失的东西,

router.post('/s3FileCallback', function(req, res) {
    debugger;
    if (req.get('x-amz-sns-message-type') == 'SubscriptionConfirmation') {
        console.log('arn' + req.get('x-amz-sns-topic-arn'));
        const subscribeUrl = req.body.SubscribeURL;
        console.log('subscribeUrl' + subscribeUrl);
    }
});

我正在使用 body 解析器作为中间件, 亚马逊在 post 请求中将 content-type 作为 text\plain 发送 感谢这个论坛,直到我遇到这个我才意识到这种类型 https://forums.aws.amazon.com/message.jspa?messageID=261061#262098

所以尝试在使用 body 解析器

之前更改 header
app.use(function(req, res, next) {
    if (req.get('x-amz-sns-message-type')) {
        req.headers['content-type'] = 'application/json';
    }
    next();
});
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: false }));

所以现在请求被解析为 json。

如果您使用的是 php,这应该有效

$res = file_get_contents('php://input');
file_put_contents('response.txt', $res. "\n", FILE_APPEND);