iJade
iJade

Reputation: 23811

Error: Invalid json using Node.js request module

I'm trying to post data using request module. Here is the code,

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

The above code throws the JSON invalid error in the terminal.

Error: Invalid json

Here is the stack trace:

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)

Upvotes: 1

Views: 1608

Answers (1)

Mockingjay
Mockingjay

Reputation: 11

Please check the JSON structure

JSON keys must be String

Update JSON Structure

var requestInfo = {"tags": [{

        "name": "UIW_IWIWU",
        "filters": {
            "attributes": {
                "VesselId": 2982
            }
        },
        "order": "asc"
    }],
    "start": "15mi-ago",
    "end": "30ms-ago"
}

Upvotes: 1

Related Questions