Alexander Mills
Alexander Mills

Reputation: 100240

NPM request module - body vs. json

I am wondering the what the difference is between the body property and the json property of the options object of the request module. For example, what is the difference between these two request instances:

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

the first one:

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

and this one:

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

for example, when I receive a post request, the JSON data is always in the body of the request. So what's the difference in assigning values to the body property of options or the json property of options, when using the request module?

Upvotes: 4

Views: 1294

Answers (1)

brandonscript
brandonscript

Reputation: 73015

The only difference is that body does not assume the content type is JSON, whereas json does, and sets the Content-Type header accordingly.

In your example, there is no difference between body: JSON.stringify(obj) and json: obj, save for the lack of automatically setting the header.

Upvotes: 6

Related Questions