How to get the content-length of the response from a request with fetch() : <p>I was getting an error when returning <code>response.json()</code> when I would do a request with an empty response body, so I'm trying to just return an empty object when there is an empty body. The approach I was going for is to check the <code>Content-Length</code> header of the response, however, somehow <code>response.headers.get('Content-Length')</code> somehow returns <code>null</code>. Here is my code:</p>

<pre><code>function fetchJSON(url, options, state = null) {
    return fetch(url, Object.assign({}, options, {
            // TODO: Add options here that should be there for every API call
            // TODO: Add token if there is one
        }))
        .then(response =&gt; {
            // Pass the JSON formatted body to the next handler
            if (response.ok === true) {
                if (response.headers.get('Content-Length') === 0) return {};
                return response.json();
            }

            // If the response was not an 2xx code, throw the appropriate error
            if (response.status === 401) throw new AuthorizationError("You are not authorized to perform this action");

            // If it is an error code that we did not expect, throw an regular error and hope that it gets noticed by
            // a developer
            throw new Error("Unexpected response: " + JSON.stringify(response));
        });
}
</code></pre>

<p>Could you maybe help me to find the <code>Content-Length</code> of the response or help me to find another approach?</p>
