Browse Source

parse the response body only if it is not empty

Fixes #170
v5.x
Luigi Pinca 9 years ago
committed by Vsevolod Strukchinsky
parent
commit
50ae11370e
  1. 2
      index.js
  2. 6
      test/json.js

2
index.js

@ -106,7 +106,7 @@ function asCallback(opts, cb) {
err = new got.HTTPError(statusCode, opts);
}
if (opts.json && statusCode !== 204) {
if (opts.json && data) {
try {
data = parseJson(data);
} catch (e) {

6
test/json.js

@ -15,8 +15,8 @@ test.before('setup', async t => {
res.end('/');
});
s.on('/204', (req, res) => {
res.statusCode = 204;
s.on('/no-body', (req, res) => {
res.statusCode = 200;
res.end();
});
@ -38,7 +38,7 @@ test('parses response', async t => {
});
test('not parses responses without a body', async t => {
const {body} = await got(`${s.url}/204`, {json: true});
const {body} = await got(`${s.url}/no-body`, {json: true});
t.is(body, '');
});

Loading…
Cancel
Save