Skip to content

Commit b69ff55

Browse files
authored
Merge pull request #1 from 3droj7/master (Fix incorrect handling of byte responses (e.g., images) by decoding base64 data)
This PR fixes an issue where binary responses (such as images) were not handled correctly in `python-tls-client-async`. Previously, the response body was treated as plain text, leading to corrupted data when downloading binary content like PNGs or PDFs. The root cause was that the underlying Go library returns binary responses encoded in base64 if `isByteResponse=true`, but the Python code did not decode this properly. This patch: - Modifies `response.py` to split and decode base64-encoded bodies. - Ensures correct handling of binary responses (e.g., images). - Sets `"isByteResponse": True` in the session payload for consistency. Related issues: - FlorianREGAZ#83 - FlorianREGAZ#119 - FlorianREGAZ#117 - FlorianREGAZ#122 Thanks to @3droj7 for PR.
2 parents f1bb7de + 974956a commit b69ff55

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

async_tls_client/response.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from typing import Union
55
import json
6+
import base64
67

78

89
class Response:
@@ -72,7 +73,7 @@ def build_response(res: Union[dict, list], res_cookies: RequestsCookieJar) -> Re
7273
# Add cookies
7374
response.cookies = res_cookies
7475
# Add response body
75-
response.text = res["body"]
76+
response.text = base64.b64decode(res["body"].split(',')[1]).decode(errors='ignore')
7677
# Add response content (bytes)
77-
response._content = res["body"].encode()
78+
response._content = base64.b64decode(res["body"].split(',')[1])
7879
return response

async_tls_client/sessions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ def build_payload():
427427
"headerOrder": self.header_order,
428428
"insecureSkipVerify": insecure_skip_verify,
429429
"isByteRequest": is_byte_request,
430+
"isByteResponse": True,
430431
"additionalDecode": self.additional_decode,
431432
"proxyUrl": final_proxy,
432433
"requestUrl": final_url,

0 commit comments

Comments
 (0)