我正在使用Node.js和Typescript从rest api获取数据。我总是得到JSON输入意外结束的错误。我的代码怎么了?
我已经对我的代码进行了字符串化,并且使用了http请求函,它正在运行。
我的json输入如下:
{"json-input":{"handler":"getContent","image":true,"video":false,"text":false,"source":"1","lage":"testlage1"}}
我的代码
api.post("/getData", (req: Request, res: any, next: NextFunction) => {
let getDataUrl = "restapiexample/?async";
console.log(getDataUrl)
let filter_data:any ={}
delete req.body['keyId'];
filter_data['json-input'] = req.body;
let jsstringData = JSON.stringify(filter_data);
console.log('###################################')
console.log(jsstringData)
console.log('###################################')
request.post(
{
url : getDataUrl,
form:jsstringData,
headers : {
"Authorization" : auth
}
},
function (error, response, body) {
if (error){
console.log("There was an error:", error);
} else {
console.log("Request posted successfully!", JSON.parse(body));
}
// res.send(jsonData.jobs[0].id)
}
);
});
错误
SyntaxError:Object.parse上JSON输入[1]的意外结束 (native)[1]在Request._callback (/home/tony/Desktop/ng2KnimeUI/dist/server/routes/api.js:49:62)[1] 在Request.self.callback (/home/tony/Desktop/ng2KnimeUI/node_modules/request/request.js:188:22) [1]在Request.emit的emitTwo(events.js:106:13)[1] (events.js:191:7)[1]请求。 (/home/tony/Desktop/ng2KnimeUI/node_modules/request/request.js:1171:10) [1]在Request.emit的emitOne(events.js:96:13)[1] (events.js:188:7)[1]在IncomingMessage。 (/home/tony/Desktop/ng2KnimeUI/node_modules/request/request.js:1091:12) [1] IncomingMessage.g(events.js:291:16)