Friday, 28 April 2017

NodeJS & Typescript - Unexpected end of JSON input

I am using Node.js and Typescript to get the data from rest api. I am alway getting an error unexpected end of JSON input. Whats wrong with my code?

I have stringify my code and with http requester it is working.

My json input looks like:

 {"json-input":{"handler":"getContent","image":true,"video":false,"text":false,"source":"1","lage":"testlage1"}}

My code

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)
        }
    );
    });

Error

SyntaxError: Unexpected end of JSON input [1] at Object.parse (native) [1] at Request._callback (/home/tony/Desktop/ng2KnimeUI/dist/server/routes/api.js:49:62) [1] at Request.self.callback (/home/tony/Desktop/ng2KnimeUI/node_modules/request/request.js:188:22) [1] at emitTwo (events.js:106:13) [1] at Request.emit (events.js:191:7) [1] at Request. (/home/tony/Desktop/ng2KnimeUI/node_modules/request/request.js:1171:10) [1] at emitOne (events.js:96:13) [1] at Request.emit (events.js:188:7) [1] at IncomingMessage. (/home/tony/Desktop/ng2KnimeUI/node_modules/request/request.js:1091:12) [1] at IncomingMessage.g (events.js:291:16)



via Tony

No comments:

Post a Comment