json 在.net core 1.0中解析AWS SNS通知

dwbf0jvd  于 2023-03-31  发布在  .NET
关注(0)|答案(2)|浏览(132)

我有一个VisualStudio17无服务器应用程序项目,正在使用.net core Web API。
我想确认我的SNS订阅,但我遇到了一个问题,AWS发送的POST请求的头部content-type设置为text/plain; charset=UTF-8,而正文是JSON。
以下是来自documentation的示例请求:

POST / HTTP/1.1
x-amz-sns-message-type: Notification
x-amz-sns-message-id: da41e39f-ea4d-435a-b922-c6aae3915ebe
x-amz-sns-topic-arn: arn:aws:sns:us-west-2:123456789012:MyTopic
x-amz-sns-subscription-arn: arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55
Content-Length: 761
Content-Type: text/plain; charset=UTF-8
Host: ec2-50-17-44-49.compute-1.amazonaws.com
Connection: Keep-Alive
User-Agent: Amazon Simple Notification Service Agent

{
  "Type" : "Notification",
  "MessageId" : "da41e39f-ea4d-435a-b922-c6aae3915ebe",
  "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic",
  "Subject" : "test",
  "Message" : "test message",
  "Timestamp" : "2012-04-25T21:49:25.719Z",
  "SignatureVersion" : "1",
  "Signature" : "EXAMPLElDMXvB8r9R83tGoNn0ecwd5UjllzsvSvbItzfaMpN2nk5HVSw7XnOn/49IkxDKz8YrlH2qJXj2iZB0Zo2O71c4qQk1fMUDi3LGpij7RCW7AW9vYYsSqIKRnFS94ilu7NFhUzLiieYr4BKHpdTmdD6c0esKEYBpabxDSc=",
  "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem",
  "UnsubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55"
}

内容类型:这使得它的解析相当困难,而一个简单的
public void Post([FromBody] string t) // or dynamic t for the matter
并抛出Request was short circuited at action filter 'Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter'.异常。
我是不是错过了什么?他们为什么要这样做,我该如何处理这个?

fhg3lkii

fhg3lkii1#

我通过将text/plain添加到JsonInputFormatter应该格式化的格式中,使其像我在this回答中描述的那样工作。

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc(config =>
    {
        foreach (var formatter in config.InputFormatters)
        {
            if (formatter.GetType() == typeof(JsonInputFormatter))
                 ((JsonInputFormatter)formatter).SupportedMediaTypes.Add(
                      MediaTypeHeaderValue.Parse("text/plain"));
        }
     });
     ...
}
toe95027

toe950272#

现在,Amazon SNS中有一个内置的解决方案,它刚刚推出了对从主题交付的HTTP消息的自定义Content-Type头的支持。https://aws.amazon.com/about-aws/whats-new/2023/03/amazon-sns-content-type-request-headers-http-s-notifications/
您必须修改Amazon SNS订阅的DeliveryPolicy属性,将headerContentType属性设置为application/json或支持的任何其他值。您可以在此处找到支持的所有值:https://docs.aws.amazon.com/sns/latest/dg/sns-message-delivery-retries.html#creating-delivery-policy

{
    "healthyRetryPolicy": {
        "minDelayTarget": 1,
        "maxDelayTarget": 60,
        "numRetries": 50,
        "numNoDelayRetries": 3,
        "numMinDelayRetries": 2,
        "numMaxDelayRetries": 35,
        "backoffFunction": "exponential"
    },
    "throttlePolicy": {
        "maxReceivesPerSecond": 10
    },
    "requestPolicy": {
        "headerContentType": "application/json"
    }
}

您可以通过调用SubscribeSetSubscriptionAttributes API操作来设置DeliveryPolicy属性:

或者,您也可以使用AWS::SNS::Subscription资源使用AWS CloudFormation来设置此策略。

相关问题