Monday, 10 April 2017

Request POST modifies object?

I'm trying to post an object with Request using post like this ->

function postData(data, cb) {
    request.post({
        url: 'http://localhost:3001/datastream',
        form: data,
    }, (err, httpResponse, body) => {
        cb(body);
    });
}

And the object looks like this:

{
    "tblPartsReport": {
        "valid": true,
        "message": "Execute SQL: SELECT * FROM tblPartsReport WHERE ID = (SELECT MAX(ID) FROM tblPartsReport); success !",
        "records": [{
            "ResourceID": 61,
            "TimeStamp": "2017-04-04T05:52:19Z",
            "PNo": 0,
            "ErrorID": 0,
            "ID": 10174
        }]
    },
    "tblMachineReport": {
        "valid": true,
        "message": "Execute SQL: SELECT * FROM tblMachineReport WHERE ID = (SELECT MAX(ID) FROM tblMachineReport); success !",
        "records": [{
            "ResourceID": 61,
            "TimeStamp": "2017-04-04T05:52:19Z",
            "AutomaticMode": true,
            "ManualMode": false,
            "Busy": false,
            "Reset": false,
            "ErrorL0": false,
            "ErrorL1": false,
            "ErrorL2": false,
            "ID": 26562
        }]
    }
}

The object is valid and ok, but after posting it looks like this on the other side:

console.log(req.body);

{
    'tblMachineReport[valid]': 'true',
    'tblMachineReport[message]': 'Execute SQL: SELECT * FROM tblMachineReport WHERE ID = (SELECT MAX(ID) FROM tblMachineReport); success !',
    'tblMachineReport[records][0][ResourceID]': '61',
    'tblMachineReport[records][0][TimeStamp]': '2017-04-04T05:52:19Z',
    'tblMachineReport[records][0][AutomaticMode]': 'true',
    'tblMachineReport[records][0][ManualMode]': 'false',
    'tblMachineReport[records][0][Busy]': 'false',
    'tblMachineReport[records][0][Reset]': 'false',
    'tblMachineReport[records][0][ErrorL0]': 'false',
    'tblMachineReport[records][0][ErrorL1]': 'false',
    'tblMachineReport[records][0][ErrorL2]': 'false',
    'tblMachineReport[records][0][ID]': '26562',
    'tblPartsReport[valid]': 'true',
    'tblPartsReport[message]': 'Execute SQL: SELECT * FROM tblPartsReport WHERE ID = (SELECT MAX(ID) FROM tblPartsReport); success !',
    'tblPartsReport[records][0][ResourceID]': '61',
    'tblPartsReport[records][0][TimeStamp]': '2017-04-04T05:52:19Z',
    'tblPartsReport[records][0][PNo]': '0',
    'tblPartsReport[records][0][ErrorID]': '0',
    'tblPartsReport[records][0][ID]': '10174'
}

Any idea why is this happening? I tried with Axios too but could not get the post working. I just want to post a regular object. Normally i have been using jQuery AJAX.



via Jack M.

No comments:

Post a Comment