【问题标题】:Node adding additional brackets to json post body节点向 json 帖子正文添加额外的括号
【发布时间】:2017-07-26 17:52:52
【问题描述】:

我正在使用名为“createPdf”的控制器将发布请求从 .NET 应用程序发送到节点 REST api

我用来发送 post 请求的 .NET 代码似乎工作正常,我使用 SerializeObject 创建一个 json 对象,然后使用 UrlEncode 对 json 进行编码,然后在 post 请求中发送它。

 Dim postjson As String = JsonConvert.SerializeObject(article)
 Dim request As WebRequest = WebRequest.Create("url")
 request.Method = "POST"
 Dim postData As String = WebUtility.UrlEncode(postjson)
 Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
 request.ContentType = "application/x-www-form-urlencoded"
 //go off and send the post request

这似乎产生了这种格式的有效 json -

{
    "source": "le sauce",
    "subTopic": "le subtopic",
    "Title": "le title",
    "Body": "le body",
    "refs": "le refs",
    "lastUpdated": "19/12/2016 11:23:56"
}

但是当我发送 json 并尝试在我的节点控制器中使用以下代码解析 json 时,它似乎在 json 中添加了额外的括号和冒号。

节点控制器

var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
var fs = require("fs");

var wkhtmltopdf = require('wkhtmltopdf');

router.use(bodyParser.urlencoded({ extended: true }));

router.post('/', function(req, res) {
   console.log(req.body); 
   res.status(200).send('lol');   
});

module.exports = router;

这是从 console.log(req.body) 输出的无效 json

{ '{
    "source”:”le sauce“,
    ”subTopic”:”le subtopic“,
    ”Title”:”le title“,
    ”Body”:”le body“,
    ”refs”:”le refs“,
    ”lastUpdated":"19/12/2016 11:23:56"
}': '' }

由于某种原因,额外的括号、冒号、引号等已在某些时候添加到 json 中并使 json 无效,我很确定它不会发生在 .NET 端,并且必须在 Node 尝试处理时发生发布请求,但我不知道在哪里。

关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: json node.js


    【解决方案1】:

    如果您发送的是 JSON,那么这是不正确的:

    request.ContentType = "application/x-www-form-urlencoded"
    

    服务器端也是这样:

    router.use(bodyParser.urlencoded({ extended: true }));
    

    这很清楚地表明您发送的是 URI 编码的表单数据,而不是 JSON,并告诉 body-parser 解码 URI 编码的表单数据,而不是 JSON。

    发送正确的内容类型:

    request.ContentType = "application/json"
    

    ...并使用正确的解析器:

    router.use(bodyParser.json());
    

    (请注意,如果您的端点需要 URI 编码的表单数据和其他需要 JSON 的端点,则可以使用多个解析器;body-parser 将从内容类型中找出答案。)

    【讨论】:

    • 由于某种原因使 req.body 输出一个空的 json 对象
    • 现在已经起作用了,不再添加额外的括号等,但发生了更奇怪的事情,在 json 中,所有引号都被替换为撇号,使 json 再次无效
    • @scriptss:当您收到它时,它不再是 JSON;这就是body-parser的point,它会为你parse body。如果你正在做console.log(req.body),你正在转储一个对象,节点将使用它的默认对象输出(例如,object { foo: 'bar' })。
    【解决方案2】:

    将您的 Content-Type 设置为 application/json。由于您尝试发送 JSON 但实际上发送的是 url 编码的数据,因此 Node 无法正确解码它。然后,试试这个代码:

    Dim httpWebRequest As HttpWebRequest = 
    (HttpWebRequest)WebRequest.Create("http://url")
    httpWebRequest.ContentType = "application/json"
    httpWebRequest.Method = "POST"
    
    Dim streamWriter As StreamWriter = new StreamWriter 
    (httpWebRequest.GetRequestStream ())
    Dim jsonData As String = "{'source':'lesauce','subTopic':'lesubtopic','Title':'letitle','Body':'lebody','refs':'lerefs','lastUpdated':'19/12/201611:23:56'}"
    streamWriter.Write (jsonData)
    streamWriter.Close ()
    
    Get Response here
    

    【讨论】:

      猜你喜欢
      • 2020-10-11
      • 2019-10-04
      • 2016-10-16
      • 2016-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-22
      相关资源
      最近更新 更多