SNS 总是发送默认消息而不是给定的特定协议消息

SNS always sends default message instead of the specific protocol message that is given

这是代码

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)

为了测试,我使用 ngrok 将本地主机(运行我的烧瓶应用程序)连接到网络并创建了一个 http 订阅。

当我发布消息时,我只能看到其中的默认消息。

知道为什么会这样吗?

您需要根据 AWS boto3 docs:

http 键的值指定为简单的 JSON 字符串值

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"
)