我正在创建一个基本的反向代理来将客户端请求传递给远程服务器。
所以我在 Node 服务器上使用了带有 express.js 的 body-parse 来允许我传递 body。
然而,JSON 中包含换行符和额外的大括号。
客户端
{
"lastRefreshedDateTime" : "2015-05-24",
"uid" : "1232141451"
}
服务器端
{
'{
\r\n"lastRefreshedDateTime": "2015-05-24",
\r\n"uid
": "1234567124321"\r\n
}\r\n': ''
}
Node.js 代码
var express = require('express');
var http = require('request');
var path = require('path');
var url = require("url");
var bodyParser = require("body-parser"); //this is required in express 4.x to output the contents of the client request body
var app = express(); //start express server
// parse application/x-www-form-urlencoded
//app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser().json())
// POST - getLocationData
app.post('/getLocationData', function (request, response)
{
console.log("Request for /getLocationData");
forwardRequest(request, response, "getLocationData", "POST", true);
});
app.listen(8080);
function forwardRequest(request, response, serviceName, requestMethod, isJSON)
{
console.log("Making request to EAS for " + serviceName + ":");
console.log(request.body); //json is fubar!
};
我已经尝试自己删除换行符,但它仍然没有解决附加大括号和倒置命令的问题