diff --git a/tls_client/response.py b/tls_client/response.py index d37a10d..ad67231 100644 --- a/tls_client/response.py +++ b/tls_client/response.py @@ -25,6 +25,8 @@ def __init__(self): # A CookieJar of Cookies the server sent back. self.cookies = cookiejar_from_dict({}) + + self._content = False def __enter__(self): return self @@ -35,6 +37,21 @@ def __repr__(self): def json(self, **kwargs): """parse response body to json (dict/list)""" return json.loads(self.text, **kwargs) + + @property + def content(self): + """Content of the response, in bytes.""" + + if self._content is False: + if self._content_consumed: + raise RuntimeError("The content for this response was already consumed") + + if self.status_code == 0: + self._content = None + else: + self._content = b"".join(self.iter_content(10 * 1024)) or b"" + self._content_consumed = True + return self._content def build_response(res: Union[dict, list], res_cookies: RequestsCookieJar) -> Response: @@ -57,4 +74,6 @@ def build_response(res: Union[dict, list], res_cookies: RequestsCookieJar) -> Re response.cookies = res_cookies # Add response body response.text = res["body"] + # Add response content (bytes) + response._content = res["body"].encode() return response