[whatwg/fetch] Remove res.json() requirement (do it by default) when content-type application/json (Issue #1489)

I propose to remove res.json() requirement with a backwards compatibility.

Fetch API already knows the content type of response and response is application/json shouldn't require an extra line `.then(res => res.json())` but do it by default. For backwards compatibility even if no longer res.json() required but is still defined, don't fall into exception, make like res.json() returning res

I think almost 100% usage of Fetch API is waiting for JSON response, and requiring `.then(res => res.json())` feels unnecessary.

Ref https://fetch.spec.whatwg.org/#fetch-api
```
fetch("https://pk.example/berlin-calling.json", {mode:"cors"})
  .then(res => {
    if(res.headers.get("content-type") &&
       res.headers.get("content-type").toLowerCase().indexOf("application/json") >= 0) {
      return res.json()
    } else {
      throw new TypeError()
    }
  }).then(processJSON)
```

insted could be
```
fetch("https://pk.example/berlin-calling.json", {mode:"cors"}).then(processJSON)
```
resulting the same result.

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

Message ID: <whatwg/fetch/issues/1489@github.com>

Received on Thursday, 22 September 2022 04:31:28 UTC