Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions pydoll/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,29 +252,31 @@ class Scripts:

// Extract cookies from set-cookie header
const cookies = document.cookie;
let text = await response.text();
const responseClone = response.clone();
const [content, text] = await Promise.all([
responseClone.arrayBuffer(),
response.text()
]);
const possiblePrefixes = [")]}}'\\n", ")]}}'\\n", ")]}}\\n"];
for (let prefix of possiblePrefixes) {{
if (text.startsWith(prefix)) {{
text = text.substring(prefix.length);
break;
}}
}}
let content, jsonData;
let jsonData;
const contentType = response.headers.get('content-type') || '';

if (contentType.includes('application/json')) {{
try {{
jsonData = JSON.parse(text);
text = JSON.stringify(jsonData);
}} catch (e) {{
// Return raw bytes if parsing fails
jsonData = null;
// Keep original text if parsing fails
}}
content = new TextEncoder().encode(text).buffer;
}} else {{
// For non-JSON, keep original text handling
content = new TextEncoder().encode(text).buffer;
// For non-JSON, return raw bytes
jsonData = null;
}}

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_tab_request_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ async def test_binary_response_exposes_content_bytes(request_tab):
assert response.status_code == 200
assert isinstance(response.content, bytes)
assert len(response.content) > 0
assert response.content == response.text.encode('utf-8')
assert response.content == PNG_1PX


@pytest.mark.asyncio
Expand Down
Loading