Parsing JSON HTTP request from Netsuite to Heroku returns error "SyntaxError: Unexpected token p"

Parsing JSON HTTP request from Netsuite to Heroku returns error "SyntaxError: Unexpected token p"

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

我创建的 json 没问题并且返回了正确的值,但是当它发送请求时,heroku 日志给我错误 SyntaxError: Unexpected token p

奇怪的是,当我 运行 在本地服务器并通过 Postman 发送时,所有这一切都很好用。当我通过导致问题的 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 在下面。这通过 Postman 有效,但通过 Netsuite 发送时无效:

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

更新: 经过更多测试后,我从 nlapiRequestURL() 中取出 headers,即使发送的信息是 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}"
}

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