0

I tried the following code to the find size of a xhr request.

var request = new XMLHttpRequest();
request.open('GET', 'authenticate.json', false);
request.send();
var size = request.getAllResponseHeaders().toLowerCase().match(/content-length: \d+/);
console.log(size + " bytes");

When I checked it with a sample ajax call, I got 188 bytes. But on the network console, the browser show 535 bytes.

enter image description here

How is the browser calculating the size of an xhr request? Is that a content length or some this else? How can I find the size using javascript instead of finding content length?

serv-inc
  • 35,772
  • 9
  • 166
  • 188
Sajith
  • 2,842
  • 9
  • 37
  • 49
  • 2
    The http headers also take up space – Musa Nov 06 '14 at 14:16
  • Most likely the response is gzipped. This might explain the difference – hindmost Nov 06 '14 at 14:17
  • Is there any way to find the size which is closer to the browser calculated size? – Sajith Nov 06 '14 at 14:51
  • @Musa how to calculate the headers size? like request.getAllResponseHeaders().length – Sajith Nov 06 '14 at 14:53
  • If you haven't yet, take a look into this thread [https://stackoverflow.com/questions/26061856/javascript-cant-access-private-properties/26063201#26063201]. depends on wich runtime that code is being executed and the server side configuration. There are 347B missing, maybe is some information (HTTP header) that the browser doesn't need to render/display. The main question could be, where that 347B was stored? [https://stackoverflow.com/questions/2659952/maximum-length-of-http-get-request] – citizen1 Oct 24 '21 at 06:04

1 Answers1

1

Most probably that is because the size shown by the Browser is total size of response body and all headers summed together whereas your Ajax call is printing the size of the response headers that will be less than total response size.

For example, here's a request I made on postman. The response size is segrated into two parts. The total size was the same 728 B when I made the request on the browser. (specified in network console)

Postman console

Community
  • 1
  • 1