簡體   English   中英

無法在Node js中使用“ content-type”:“ text / plain; charset = UTF-8”

[英]Unable to Print a JSON Object in Node js with “content-type”: “text/plain; charset=UTF-8”

我正在嘗試使用以下命令在req.body中打印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:該點2 文件其通知

使用JSON解析器來處理將轉義的控制字符表示形式轉換回其ASCII字符值(例如,將\\ n轉換為換行符)。 您可以使用現有的JSON解析器,例如Jackson JSON Processor( http://wiki.fasterxml.com/JacksonHome ),也可以編寫自己的解析器。

編輯1:

您可能會看到打印的標題,它們具有content-type": "text/plain; charset=UTF-8 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()確實打印出了對象。 該對象只是空的,即{}。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM