Get data size of HTTP request and response

9.7k views Asked by At

I'm trying to manage my Meteor web app's API usage and have two questions.

  1. How do I know how the size of the incoming POST request data? Note that I don't care about the contribution from headers; I just want to know the size of the data part.
  2. How do I calculate a priori the amount of data that will be transferred if I respond to an HTTP call with JSON?

I am open to using Iron Router or Node's HTTP package. I previously asked about setting some limits, but in this case I want to calculate and keep track of how much data is going in and out of my app.

1

There are 1 answers

0
Hyo Byun On BEST ANSWER
  1. You could get the content header element content-length, but from what I understand, this field isn't guaranteed. Could you check the size of the retrieved POST data after it has arrived? Node also specifies that

Note: Node does not check whether Content-Length and the length of the body which has been transmitted are equal or not. https://nodejs.org/api/http.html#http_class_http_clientrequest

  1. You could get the length of the string as the bytes after the json is serialized. But I don't think this will be an accurate representation of bandwidth use, considering compression. If you are interested in memory, you could use object-sizeof(https://www.npmjs.com/package/object-sizeof). This module takes assumptions based on the memory allocation of the objects, so its probably ill fitted for this purpose.