1

I'm trying to calculate the response size (content length) of a XMLHttpRequest. This works fine when 'Content-Length' is in the response header. However, by using Chrome Developer Tool, I noticed that when Transfer Encoding is chunked (when response is over 36 KB), I'm not able to get response size.

How can I estimate the response size when Transfer Encoding is chunked in JavaScript?

        let req = new XMLHttpRequest();
        req.open("POST", "localhost:8080/hello");
        req.setRequestHeader("Content-type", "application/json");
        req.responseType = "json";

        req.onloadend = (e) => {
            console.log(e.total);
        };

        req.send({ world: "!"});
cheninator
  • 11
  • 4
  • Is this - https://stackoverflow.com/questions/3304126/chunked-encoding-and-content-length-header - related? – raina77ow Dec 13 '17 at 21:29
  • You are sending `null`. Why are you setting a Content-Type? No data at all is not valid JSON. – Quentin Dec 13 '17 at 21:46
  • Oh! Sorry for the mistake in the code. I wanted to send {} and not null. @raina77ow, So if I understand, I have to create a custom header which contains the response size? So how does the Chrome Network Developper tool is able to calculate each HTTP response size? – cheninator Dec 14 '17 at 15:55

0 Answers0