无法打印节点js中带有“的JSON对象”;内容类型";:&引用;文本/纯文本;字符集=UTF-8“;
我试图使用以下命令在NodeJS中打印JSON对象,即无法打印节点js中带有“的JSON对象”;内容类型";:&引用;文本/纯文本;字符集=UTF-8“;,json,node.js,amazon-web-services,amazon-sns,Json,Node.js,Amazon Web Services,Amazon Sns,我试图使用以下命令在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
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步:第2点中,它建议
使用处理转换转义表示的JSON解析器
将控制字符恢复为其ASCII字符值(用于
例如,将\n转换为换行符)。你可以使用
现有的JSON解析器,如Jackson JSON处理器
()或者自己写
编辑1:
正如您可能看到的,打印的标题有内容类型“:“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"
}
stringify()确实打印出了对象。对象只是空的,即{}。JSON.stringify()打印出了对象。对象只是空的,即{}。什么不起作用?在您的示例中,
req.body
是一个空对象。console.log
接受多个参数。如果您像console.log('printing body,req.body)
那样调用它,您将得到写入stdout的完整对象。但正如@alexmac提到的,它看起来像req.body
是空的,可能是重复的:这很奇怪,它应该输出您想要的内容。你确定req.body
有数据吗?尝试在app.use(bodyParser.text({type:“text/plain”})之后添加app.use(bodyParser.urlencoded({extended:true}))代码>我认为这是增加对文本/普通内容支持的方法。看到什么不起作用了吗?在您的示例中,req.body
是一个空对象。console.log
接受多个参数。如果您像console.log('printing body,req.body)
那样调用它,您将得到写入stdout的完整对象。但正如@alexmac提到的,它看起来像req.body
是空的,可能是重复的:这很奇怪,它应该输出您想要的内容。你确定req.body
有数据吗?尝试在app.use(bodyParser.text({type:“text/plain”})之后添加app.use(bodyParser.urlencoded({extended:true}))代码>我认为这是增加对文本/普通内容支持的方法。看见