【发布时间】:2017-07-12 11:45:14
【问题描述】:
我正在尝试使用请求模块发布数据。这是代码,
var requestInfo = {
tags: [
{
"name": 'UIW_IWIWU',
"filters": {
"attributes": {
"VesselId": '2982'
}
},
"order": "asc"
}
],
start: "15mi-ago",
end: "30ms-ago"
};
request.post({
uri:'http://localhost:3000/data-api',
json: 'true',
body: requestInfo
},function (error, response, body) {
res.send(response);
}
);
以上代码在终端抛出JSON无效错误。
Error: Invalid json
这是堆栈跟踪:
Error: invalid json
at parse (D:\Working\branches\Smartship-SNG_Demo\node_modules\body-parser\li
b\types\json.js:83:15)
at D:\Working\branches\Smartship-SNG_Demo\node_modules\body-parser\lib\read.
js:108:18
at invokeCallback (D:\Working\branches\Smartship-SNG_Demo\node_modules\raw-b
ody\index.js:262:16)
at done (D:\Working\branches\Smartship-SNG_Demo\node_modules\raw-body\index.
js:251:7)
at IncomingMessage.onEnd (D:\Working\branches\Smartship-SNG_Demo\node_module
s\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)
【问题讨论】:
-
它也适用于我,也适用于终端。节点版本:7.10,如果有帮助的话。
-
共享错误堆栈跟踪。
-
@MukeshSharma 堆栈跟踪已更新
-
我认为,问题在于
request.post的回复。http://localhost:3000/data-api可能会返回非 json 数据,并将application/json设置为内容类型。