1

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)
4
  • 1
    It works for me, in teminal too. Node version: 7.10, if it helps. Commented Jul 12, 2017 at 11:52
  • 1
    share error stack trace. Commented Jul 12, 2017 at 11:53
  • 1
    @MukeshSharma stack trace updated Commented Jul 12, 2017 at 11:55
  • I think, issue is in the response of request.post. http://localhost:3000/data-api may be returning non-json data with setting application/json as content-type. Commented Jul 12, 2017 at 13:08

1 Answer 1

1

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"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.