Home > Back-end >  SNS always sends default message instead of the specific protocol message that is given
SNS always sends default message instead of the specific protocol message that is given

Time:11-10

Here is the code

message = {
   "default":"Sample fallback message",
   "http":{
      "data":[
         {
            "type":"articles",
            "id":"1",
            "attributes":{
               "title":"JSON:API paints my bikeshed!",
               "body":"The shortest article. Ever.",
               "created":"2015-05-22T14:56:29.000Z",
               "updated":"2015-05-22T14:56:28.000Z"
            }
         }
      ]
   }
}

message_as_json = json.dumps(message)

response = sns_client.publish(TopicArn = "arn:aws:sns:us-east-1:MY-ARN",
            Message = message_as_json, MessageStructure = "json")

print(response)

To test, I used ngrok to connect the localhost (which runs my flask app) to the web and created a http subscription.

When I publish the message I can only see default message in that.

Any idea why this happens?

CodePudding user response:

You need to specify the value of the http key as a simple JSON string value according to the AWS boto3 docs:

Keys in the JSON object that correspond to supported transport protocols must have simple JSON string values.

Non-string values will cause the key to be ignored.

import json
import boto3

sns_client = boto3.client("sns")

message = {
    "default": "Sample fallback message",
    "http": json.dumps(
        {
            "data": [
                {
                    "type": "articles",
                    "id": "1",
                    "attributes": {
                        "title": "JSON:API paints my bikeshed!",
                        "body": "The shortest article. Ever.",
                        "created": "2015-05-22T14:56:29.000Z",
                        "updated": "2015-05-22T14:56:28.000Z",
                    },
                }
            ]
        }
    ),
}

response = sns_client.publish(
    TopicArn="arn:aws:sns:us-east-1:MY-ARN", Message=json.dumps(message), MessageStructure="json"
)
  • Related