Re: [whatwg/fetch] 204 and json (#113)

If we consider:
```js
const data = await (/^application\/json/.test(r.headers.get('content-type')) ? r.json() : r.text());
```
(from https://gist.github.com/caub/7494b4391c2d62c49b565d2cfc2c0c1f#file-fetch-wrapper-js-L17)

since we can't consume the body twice, we can't do a `try { data = await r.json() } catch { data = await r.text() }`

And since content-type is not fully reliable, is it better to do do?
```js
let data = await r.text();
try {
  data = JSON.parse(data);
} catch {}
```
in terms of performance? of readability, etc.

I know an answer would be to ensure the server always return json responses (or empty with/without the right statusCode), but it's not always the case

I see why the idea of `Response.prototype.json` emerged, as a shortcut for `JSON.parse(await r.text())`, but it's not really ideal in practice as you can see

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/113#issuecomment-524215812

Received on Friday, 23 August 2019 08:02:06 UTC