Set parsing of body to true by default

45 views
Skip to first unread message

Adrian Lynch

unread,
Aug 28, 2014, 5:14:39 PM8/28/14
to nod...@googlegroups.com
Can I get a check sanity check on my usage here?

I want to default all requests to parse the body as JSON by setting the the option in the defaults like so:

var request = require("request");

request.defaults({
json: true // Seems to be ignored
});

request({
//json: true // Seems to be needed
}, function(err, res, body) {
console.log(body);
console.log(body.accessToken);
});

Body doesn't get parsed.

If I add it to the actual request options object, it does.

Am I misusing defaults() in some way?

Thanks.

greelgorke

unread,
Sep 1, 2014, 5:25:53 AM9/1/14
to nod...@googlegroups.com
as you can see here: https://github.com/mikeal/request#requestdefaultsoptions defaults actually returns a new request object with your set defaults. so if you want to use it this way you have to do so:

var request = require("request");

var defaultRequest = request.defaults({
json: true // Seems to be ignored
});

defaultRequest({
}, function(err, res, body) {
console.log(body);
console.log(body.accessToken);
});



Adrian Lynch

unread,
Sep 12, 2014, 6:23:04 AM9/12/14
to nod...@googlegroups.com
Thanks G!

I'd read this line more than once but it didn't sink in:

"This method *returns a wrapper* around the normal request API that defaults to whatever options you pass in to it."

Adrian

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/9183336a-6dce-4b69-8279-ffa5fb9062e7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages