Repository: sindresorhus/got (ts)

Issue:
ParseError on response with no body
I'm using `got` to query a JSON API where some endpoints simply return `200` with no body.

`got` throws a [`ParseError`](https://github.com/sindresorhus/got/blob/3dc219e70474e54c4ec4b2c27468f6ff722f8c6b/index.js#L107) because it only skips parsing if the status code is `204`.

I wonder if it makes sense to skip parsing every time the body is empty, without checking the status code.

```js
if (opts.json && res.body) {
  // try to parse
}
```

I'm happy to send a pull request if you think the above change would be useful.

Pull request description:
Parse the response body only if it is not empty
Fixes #170.

Patch:
diff --git a/index.js b/index.js
index f54f3b9..1a4d91d 100644
--- a/index.js
+++ b/index.js
@@ -101,7 +101,7 @@ function asPromise(opts) {
 
 					res.body = data;
 
-					if (opts.json && statusCode !== 204) {
+					if (opts.json && res.body) {
 						try {
 							res.body = JSON.parse(res.body);
 						} catch (e) {


Test patch:
diff --git a/test/json.js b/test/json.js
index 2f0b2dc..a30b8d7 100644
--- a/test/json.js
+++ b/test/json.js
@@ -15,8 +15,8 @@ test.before('setup', async () => {
 		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, '');
 });