简体   繁体   English

请求POST修改对象?

[英]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. 我也尝试过Axios,但无法正常工作。 I just want to post a regular object. 我只想发布一个常规对象。 Normally i have been using jQuery AJAX. 通常我一直在使用jQuery AJAX。

EDIT: This was the correct way :) 编辑:这是正确的方法:)

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

I assume that you use the node module request. 我假设您使用节点模块请求。 You have to use the json option not the form option. 您必须使用json选项而非form选项。 Set json to true and place your data in body not in form. 将json设置为true,然后将数据放在窗体中,而不放在窗体中。

body - entity body for PATCH, POST and PUT requests. body-PATCH,POST和PUT请求的实体主体。 Must be a Buffer, String or ReadStream. 必须是Buffer,String或ReadStream。 If json is true, then body must be a JSON-serializable object. 如果json为true,则body必须是JSON可序列化的对象。

form - when passed an object or a querystring, this sets body to a querystring representation of value, and adds Content-type: application/x-www-form-urlencoded header. form-传递对象或查询字符串时,会将主体设置为值的查询字符串表示形式,并添加Content-type:application / x-www-form-urlencoded标头。 When passed no options, a FormData instance is returned (and is piped to request). 如果不传递任何选项,则将返回FormData实例(并将其通过管道传递给请求)。 See "Forms" section above. 请参阅上面的“表格”部分。

https://github.com/request/request#requestoptions-callback https://github.com/request/request#requestoptions-callback

The output, which you included in your post, is in the format of application/x-www-form-urlencoded as far as I remember PHP and other pure server-side languages prefer this format. 据我记得,PHP和其他纯服务器端语言更喜欢这种格式,您在帖子中包含的输出采用application/x-www-form-urlencoded格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM