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

法学硕士

我有一个VisualStudio17无服务器应用程序项目,并且正在使用.net核心Web Api。

我想确认我的SNS订阅,但是我遇到一个问题,即AWS在正文为JSON时发送标头content-type设置为的POST请求text/plain; charset=UTF-8

这是他们文档中的示例请求

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

内容类型:文本,正文JSON。这使得解析非常困难,而且简单

public void Post([FromBody] string t) // or dynamic t for the matter

不起作用并引发Request was short circuited at action filter 'Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter'.异常。

我想念什么吗?他们为什么这样做,我该如何处理?

法学硕士

通过添加应格式化的格式,使它像我在答案中描述的那样工作text/plainJsonInputFormatter

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章