parsing从Netsuite到Heroku的JSON HTTP请求会返回错误“SyntaxError:Unexpected token p”

我创build了一个接受参数的Portlet表单脚本,将其提交给适当的Suitelet,然后将生成的JSON从Netsuite转发到Node.js Heroku服务器以供进一步处理。

我创build的JSON是好的,并返回正确的值,但是当它发送请求,heroku日志给我的错误SyntaxError: Unexpected token p

奇怪的是,当我在本地运行服务器并通过邮递员发送时,所有这些工作都很好。 当我通过Netsuite请求发送所有内容时,会造成问题。 它也可能是body-parser的错误引用body-parser的东西。 我觉得这个答案是正确的,但最终并没有帮助。 任何想法如何纠正?

处理JSON和请求的Suitelet POST请求:

  var json = { "par": { "id":request.getParameter("custpage_id"), "num":request.getParameter("custpage_num") }, "in": { "headers":temp.toString() } }; for (var i = 0; i < info.length; i++){ var newVal = "val" + (i+1); json.in[newVal] = info[i]; } nlapiRequestURL("https://somelink.herokuapp.com/", json, {"Content-Type":"application/json"}); 

Heroku代码:

 app.use(cors()); app.use(bodyParser.urlencoded({extended: false})); app.use(bodyParser.json()); var server = app.listen(process.env.PORT || 8080, function() { var port = server.address().port; console.log('port #: ', port); }); app.post('/', function( req, res) { console.log(JSON.stringify(req.headers)); console.log(req.body); }); 

完整的错误:

 SyntaxError: Unexpected token p at parse (/app/node_modules/body-parser/lib/types/json.js:83:15) at invokeCallback (/app/node_modules/raw-body/index.js:262:16) at /app/node_modules/body-parser/lib/read.js:116:18 at done (/app/node_modules/raw-body/index.js:251:7) at IncomingMessage.onEnd (/app/node_modules/raw-body/index.js:307:7) at emitNone (events.js:86:13) at IncomingMessage.emit (events.js:185:7) at endReadableNT (_stream_readable.js:974:12) at _combinedTickCallback (internal/process/next_tick.js:74:11) at process._tickCallback (internal/process/next_tick.js:98:9) 

编辑:应该打印出来的JSON如下。 这是通过邮递员,但不是当它通过Netsuite发送:

 { "par": { "id": "customID", "num": "132" }, "in": { "headers": "Item,Current", "val1": "98696,7938", "val2": "58839,936" } } 

更新:经过一些testing后,我从nlapiRequestURL()取出头文件,即使发送的信息是JSON,请求默认为"content-type":"application/x-www-form-urlencoded; charset=UTF-8" 。 更新后的请求如下所示: nlapiRequestURL("https://somelink.herokuapp.com/", json);

 The Heroku POST request code now: app.post('/', function( req, res) { console.log(JSON.stringify(req.headers)); console.log(JSON.stringify(req.body)); }); 

这发送JSON,但格式是错误的。 而不是应该显示在我的编辑中的JSON,我得到下面:

 { "par": "{id=customID,num=132}", "in": "{headers=Item,Current, val1=98696,7938, val2=58839,936}" } 

事实certificate,我需要做的是在我发送数据之前将创build的JSON对象进行string化。 所以只是做nlapiRequestURL("https://somelink.herokuapp.com/", JSON.stringify(json)); 完美的工作!