【发布时间】:2017-09-04 22:26:59
【问题描述】:
我正在尝试使用以下命令在 NodeJS 中打印 JSON 对象,即 req.body,但使用 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 步中:它建议的 document 第 2 点
使用处理转义表示的 JSON 解析器 控制字符返回到它们的 ASCII 字符值(对于 例如,将 \n 转换为换行符)。你可以使用一个 现有的 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"
}
【问题讨论】:
-
什么不起作用?在您的示例中,
req.body是一个空对象。 -
console.log接受多个参数。如果您将其称为console.log('printing body, req.body),您将获得写入标准输出的完整对象。但正如@alexmac 提到的,看起来req.body是空的 -
这很奇怪,它应该输出你想要的。你确定
req.body有数据吗? -
尝试在
app.use(bodyParser.urlencoded({ extended: true }));之后添加app.use(bodyParser.text({type:"text/plain"}))我认为这是添加对文本/纯内容的支持的方法。见here
标签: json node.js amazon-web-services amazon-sns