Skip to content

fix: handle missing content-type header in Request.post_data_json#3

Closed
Skn0tt wants to merge 1 commit into
mainfrom
skn0tt/vigilant-potato
Closed

fix: handle missing content-type header in Request.post_data_json#3
Skn0tt wants to merge 1 commit into
mainfrom
skn0tt/vigilant-potato

Conversation

@Skn0tt

@Skn0tt Skn0tt commented Jun 9, 2026

Copy link
Copy Markdown
Owner

Problem

Request.post_data_json indexed self.headers["content-type"], which raises KeyError when a POST request has a body but no content-type header:

KeyError: 'content-type'

The documented behavior is to parse form-urlencoded bodies when that content-type is set, and otherwise fall back to JSON parsing. A missing header should take the JSON fallback path. This matches the upstream JS implementation, where this.headers()['content-type'] is undefined-safe before the form-urlencoded branch.

Closes microsoft#3093

Fix

In playwright/_impl/_network.py, read the header defensively and guard the branch:

content_type = self.headers.get("content-type")
if content_type and "application/x-www-form-urlencoded" in content_type:
    return dict(parse.parse_qsl(post_data))

Tests

Added to tests/async/test_network.py:

  • test_should_parse_the_json_post_data_when_content_type_header_is_missing — the regression guard. Uses request.headers.delete('content-type') to produce a request whose content-type key is genuinely absent (unlike setting it to '', which keeps an empty-valued key), then asserts post_data_json returns the parsed JSON. Verified to fail with KeyError on the unpatched code.
  • test_should_return_post_data_for_put_requests — mirrors the upstream network-post-data suite for parity.

All existing network tests continue to pass.

Notes

  • No public API surface change, so no codegen / api.json updates are needed.

post_data_json indexed self.headers["content-type"], raising KeyError
when a POST request had a body but no content-type header. Read the
header defensively with .get() and guard the form-urlencoded branch so
a missing header falls through to JSON parsing, matching the upstream
JS implementation.

Closes microsoft#3093

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Skn0tt

Skn0tt commented Jun 9, 2026

Copy link
Copy Markdown
Owner Author

Wrong base repo — reopening against microsoft/playwright-python.

@Skn0tt Skn0tt closed this Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Request.post_data_json raises KeyError when content-type is missing

1 participant