Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of raw response in bytes

I need to make a HTTP request and determine the response size in bytes. I have always used request for simple HTTP requests, but I am wondering if I can achieve this using raw?

>>> r = requests.get('https://github.com/', stream=True) >>> r.raw 

My only problem is I don't understand what raw returns or how I could count this data-type in bytes? Is using request and raw the right approach?

like image 586
ewhitt Avatar asked Jul 11 '14 00:07

ewhitt


People also ask

How do I know my response size?

You can Use HTTPRequest and HTTPResponse class method getBodyAsBlob(); to get the response body in blob and then you can check the size. httpRequest. getBodyAsBlob(). size(); httpResponse.

How large can a get response be?

The response limit is 9,223,372,036,854,775,807 bytes (2 ** 64).

Is content-Length required in HTTP response?

A valid Content-Length field value is required on all HTTP/1.0 request messages containing an entity body.


1 Answers

Just take the len() of the content of the response:

>>> response = requests.get('https://github.com/') >>> len(response.content) 51671 

If you want to keep the streaming, for instance if the content is (too) large you can iterate over chunks of the data and sum their sizes:

>>> with requests.get('https://github.com/', stream=True) as response: ...     size = sum(len(chunk) for chunk in response.iter_content(8196)) >>> size 51671 
like image 151
BlackJack Avatar answered Sep 28 '22 04:09

BlackJack