Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws sns confirm subscription request processing issue

I am trying to implement the aws sns service for a bucket in s3 and i am following this document https://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.html according to this there will be subscribe url in the request for the confirmation subscription which will be coming to the url that we provide, but i am receiving empty body in the request. I tried to log the body but gave me an empty object. and tried by using the bodyparser but same result.

here is my route that i am implementing.

 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);
})

is there any thing i am missing. can any one point me in right direction please.

like image 684
Manoj Avatar asked Jul 25 '18 02:07

Manoj


People also ask

How do I confirm my SNS subscription?

To confirm the subscription and start receiving notifications at the endpoint, you must visit the SubscribeURL URL (for example, by sending an HTTP GET request to the URL).

How do I confirm my SNS subscription to SQS?

To confirm the subscription, you can use the Amazon SQS console or the ReceiveMessage action. Before you subscribe an endpoint to the topic, make sure that the queue can receive messages from the topic by setting the sqs:SendMessage permission for the queue.

How do I cancel a pending SNS subscription on AWS?

Sign in to the Amazon SNS console . In the left navigation pane, choose Subscriptions. On the Subscriptions page, select a subscription with a Status of Confirmed, and then choose Delete. In the Delete subscription dialog box, choose Delete.

Which port is used by SNS to send the notification?

The Amazon SNS Topic external action has two output ports. The success port (✔) can send a message if the Amazon SNS topic is notified successfully. The failure port (x) can send a message if the notification fails.


2 Answers

I found what i was missing,

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);
    }
});

I am using body parser as a middleware, amazon is sending content-type as text\plain in the post request thanks for this forum i did not realize the type until i came across this https://forums.aws.amazon.com/message.jspa?messageID=261061#262098

so tried a work around to change the header before using the bodyparser

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 }));

so now the req is parsed as json.

like image 138
Manoj Avatar answered Oct 10 '22 03:10

Manoj


In case if you are using php, this should work

$res = file_get_contents('php://input');
file_put_contents('response.txt', $res. "\n", FILE_APPEND);
like image 25
dav Avatar answered Oct 10 '22 02:10

dav