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: "!"}); 
3
  • Is this - stackoverflow.com/questions/3304126/… - related? Commented Dec 13, 2017 at 21:29
  • You are sending null. Why are you setting a Content-Type? No data at all is not valid JSON. Commented Dec 13, 2017 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? Commented Dec 14, 2017 at 15:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.