NPM 请求模块 - 正文与 json

NPM request module - body vs. json

我想知道请求模块的选项对象的主体 属性 和 json 属性 之间有什么区别。比如这两个请求实例有什么区别:

var obj = {
        "type": "SCHEDULED_CALLBACK",
        "appointmentTime": "2014-10-06T15:45:00Z",
        "queue": queueName
    };

第一个:

  request.post({
            method: 'POST',
            uri: url,
            headers: {'content-type': 'application/json'},
            json: obj  
        }
        , function (err, response, body) {
            cb(err, response, body);
        });

还有这个:

request.post({
        method: 'POST',
        qs: {queue: queueName}, //query string params go here
        uri: url,
        body: JSON.stringify(obj),
    }
    , function (err, response, body) {
        cb(err, response, body);
    });

例如,当我收到一个post请求时,JSON数据总是在请求的正文中。那么在使用请求模块时,将值分配给选项的主体 属性 或选项的 json 属性 有什么区别?

唯一的区别是 body 不假定内容类型为 JSON,而 json 假定内容类型,并相应地设置 Content-Type header .

在您的示例中,body: JSON.stringify(obj)json: obj 之间没有区别,只是没有自动设置 header。