Skip to content

[Bug]: Request.post_data raises UnicodeDecodeError for non-UTF8 request bodies #3098

Description

@rohan-patnaik

Version

main branch as of 2026-06-09

Steps to reproduce

Send a request with binary bytes that are not valid UTF-8, then read request.post_data:

async with page.expect_request("**/upload") as request_info:
    await page.evaluate("""
        () => fetch('/upload', {
            method: 'POST',
            body: new Uint8Array([255, 254])
        })
    """)
request = await request_info.value
print(request.post_data)

Expected behavior

request.post_data should not crash. It can return a best-effort UTF-8 string, while request.post_data_buffer remains the exact binary-safe API.

This would match JavaScript behavior, where Buffer.toString("utf-8") replaces invalid bytes instead of throwing.

Actual behavior

Python decodes request body bytes with strict UTF-8 decoding, so request.post_data can raise:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff ...

Potential fix direction

Decode text post data with replacement, for example decode("utf-8", errors="replace"), and add a regression test that verifies:

  • request.post_data does not raise for invalid UTF-8 bytes
  • request.post_data_buffer still returns the exact original bytes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions