Browse Source

Merge pull request #45 from sindresorhus/json-option

Add json option
http2
Kevin Mårtensson 10 years ago
parent
commit
ef78510bef
  1. 12
      index.js
  2. 9
      readme.md
  3. 48
      test/test-json.js

12
index.js

@ -40,11 +40,13 @@ function got(url, opts, cb) {
var encoding = opts.encoding; var encoding = opts.encoding;
var body = opts.body; var body = opts.body;
var json = opts.json;
var proxy; var proxy;
var redirectCount = 0; var redirectCount = 0;
delete opts.encoding; delete opts.encoding;
delete opts.body; delete opts.body;
delete opts.json;
if (body) { if (body) {
opts.method = opts.method || 'POST'; opts.method = opts.method || 'POST';
@ -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) { function get(url, opts, cb) {
var parsedUrl = urlLib.parse(prependHttp(url)); var parsedUrl = urlLib.parse(prependHttp(url));
var fn = parsedUrl.protocol === 'https:' ? https : http; var fn = parsedUrl.protocol === 'https:' ? https : http;
@ -116,6 +122,12 @@ function got(url, opts, cb) {
read(res, encoding, function (err, data) { read(res, encoding, function (err, data) {
if (err) { if (err) {
err = new GotError('Reading ' + url + ' response failed', 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); cb(err, data, response);

9
readme.md

@ -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`. 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 ##### options.timeout
Type: `number` Type: `number`

48
test/test-json.js

@ -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();
});
Loading…
Cancel
Save