>我正在尝试打印JSON对象,即req.body
使用以下命令在 NodeJS 中,但使用 JSON.stringify 无济于事。有人可以建议/指导我如何打印这个 JSON 对象req.body
console.log("printing body: "+JSON.stringify(req.body))
console.log("printing body: "+req.body)
console.log("printing headers: "+JSON.stringify(req.headers))
输出:
printing body: {}
printing body: [object Object]
printing headers: {
"x-amz-sns-message-type": "SubscriptionConfirmation",
"x-amz-sns-message-id": "3dd623ert-7203-4e12-bf11-36589f9dce65",
"x-amz-sns-topic-arn": "arn:aws:sns:us-west-2:33030356879323:testTopic10",
"content-length": "1520",
"content-type": "text/plain; charset=UTF-8",
"host": "example.com",
"connection": "Keep-Alive",
"user-agent": "Amazon Simple Notification Service Agent",
"accept-encoding": "gzip,deflate"
}
解析 Amazon SNS 提供的 JSON 对象的准则:
在步骤 1:本文档的第 2 点中,它建议
使用处理转义表示转换的 JSON 解析器 控制字符返回到其 ASCII 字符值(对于 例如,将 转换为换行符)。您可以使用 现有的 JSON 解析器,例如 Jackson JSON 处理器 (http://wiki.fasterxml.com/JacksonHome)或编写自己的。
编辑 1:
如您所见 打印的标题,它们有content-type": "text/plain; charset=UTF-8
,这可能是bodyparser.json()
不起作用并且我们无法使用JSON.stringify()
的原因
编辑 2
这是我的身体解析代码。
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // Used to parse the JSON request
app.use(bodyParser.urlencoded({ extended: true }));
编辑3:
请求中发送的原始正文的示例
{
"Type" : "SubscriptionConfirmation",
"MessageId" : "4c3232a-f297-4fba-96e8-dc821a0b2621",
"Token" : "2336412f37fb687f5d51e6e241d59b68c4e23a8e1a7b89aecf0dd7e227b0cf8ce107c9d1a4216d1aaf7dcdf66c18f0e06f1811a98351ced5018395453fee6f7e12fd5962220e0a81431063914e7b8d0c5340baeaf9dd2fe12e5288fbb88405fca2136c026d2b04e709e8ab6",
"TopicArn" : "arn:aws:sns:us-west-2:3303035234123:testTopic10",
"Message" : "You have chosen to subscribe to the topic arn:aws:sns:us-west-2:33030123453:testTopic10.nTo confirm the subscription, visit the SubscribeURL included in this message.",
"SubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-2:33030234243413:testTopic10&Token=2336412f37fb687f5d51e6e241d59b68c4e58148956199a8e1a7be0dd7e227b0cfer1aaf7dcdf66c18f0e06f1811a98351ced5018395453fee6f7e12fd5962220e0a81431063914e7b8d0c5340baeaf9dd2fe12e5288fbb88405fca2136c026d2b04e709e8ab6",
"Timestamp" : "2017-09-04T13:06:36.005Z",
"SignatureVersion" : "1",
"Signature" : "QRy9574PIfSuNReyGEgDO86/utgF7R5enCmQTYBsUIdN0ohF9jWzh+qU9FLDp7EIXzg6Q3bLoI3HeYzNE4iMLHATixf2Iz29e0/ekWaMBewj+Q+pt42tKDh9YndRmyE2CSRJ7LTnvTVpS3MUgDI/kaQKmThxgN9wb8y8gebojuIE6zNAbYmuVVA+W6rIiF+dyG9e+f89dWSSReITB19XaVtLZ/BrcQWRyrBRFE06lXxYuGaLUIfTvItleaxX/BxKnNdxUL04sRNQ==",
"SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-433026a4050d206028891123453da859041.pem"
}
JSON.stringify() 确实打印出了对象。对象只是空的,即 {}。
Amazon SNS 中现在有一个内置解决方案。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"
}
}
您可以通过调用Subscribe
或SetSubscriptionAttributes
API 操作来设置DeliveryPolicy
属性:
- https://docs.aws.amazon.com/sns/latest/api/API_Subscribe.html
- https://docs.aws.amazon.com/sns/latest/api/API_SetSubscriptionAttributes.html
或者,您也可以使用 AWS CloudFormation 来设置此策略,并使用AWS::SNS::Subscription
资源。