Skip to content

Commit

Permalink
Merge pull request #45 from sindresorhus/json-option
Browse files Browse the repository at this point in the history
Add json option
  • Loading branch information
kevva committed Apr 6, 2015
2 parents e935226 + f8057d2 commit ef78510
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ function got(url, opts, cb) {

var encoding = opts.encoding;
var body = opts.body;
var json = opts.json;
var proxy;
var redirectCount = 0;

delete opts.encoding;
delete opts.body;
delete opts.json;

if (body) {
opts.method = opts.method || 'POST';
Expand All @@ -61,6 +63,10 @@ function got(url, opts, cb) {
};
}

if (proxy && json) {
throw new GotError('got can not be used as stream when options.json is used');
}

function get(url, opts, cb) {
var parsedUrl = urlLib.parse(prependHttp(url));
var fn = parsedUrl.protocol === 'https:' ? https : http;
Expand Down Expand Up @@ -116,6 +122,12 @@ function got(url, opts, cb) {
read(res, encoding, function (err, data) {
if (err) {
err = new GotError('Reading ' + url + ' response failed', err);
} else if (json) {
try {
data = JSON.parse(data);
} catch (e) {
err = new GotError('Parsing ' + url + ' response failed', err);
}
}

cb(err, data, response);
Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ _This option and stream mode are mutually exclusive._

Body, that will be sent with `POST` request. If present in `options` and `options.method` is not set - `options.method` will be set to `POST`.

##### options.json

Type: `Boolean`
Default: `false`

_This option and stream mode are mutually exclusive._

If enabled, response body will be parsed with `JSON.parse`.

##### options.timeout

Type: `number`
Expand Down
48 changes: 48 additions & 0 deletions test/test-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';
var tape = require('tape');
var got = require('../');
var server = require('./server.js');
var s = server.createServer();

s.on('/', function (req, res) {
res.end('{"data":"dog"}');
});

s.on('/invalid', function (req, res) {
res.end('/');
});


tape('setup', function (t) {
s.listen(s.port, function () {
t.end();
});
});

tape('json option can not be used in stream mode', function (t) {
t.throws(function () {
got(s.url, {json: true});
}, 'got can not be used as stream when options.json is used');
t.end();
});

tape('json option should parse response', function (t) {
got(s.url, {json: true}, function (err, json) {
t.error(err);
t.deepEqual(json, {data: 'dog'});
t.end();
});
});

tape('json option wrap parsing errors', function (t) {
got(s.url + '/invalid', {json: true}, function (err) {
t.ok(err);
t.equal(err.message, 'Parsing ' + s.url + '/invalid response failed');
t.end();
});
});

tape('cleanup', function (t) {
s.close();
t.end();
});

0 comments on commit ef78510

Please sign in to comment.