diff --git a/playwright/_impl/_network.py b/playwright/_impl/_network.py index 06bf88267..5a840f63f 100644 --- a/playwright/_impl/_network.py +++ b/playwright/_impl/_network.py @@ -220,8 +220,8 @@ def post_data_json(self) -> Optional[Any]: post_data = self.post_data if not post_data: return None - content_type = self.headers["content-type"] - if "application/x-www-form-urlencoded" in content_type: + 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)) try: return json.loads(post_data) diff --git a/tests/async/test_network.py b/tests/async/test_network.py index 8747956ab..e88b24c24 100644 --- a/tests/async/test_network.py +++ b/tests/async/test_network.py @@ -468,6 +468,46 @@ async def test_should_throw_on_invalid_json_in_post_data( assert "POST data is not a valid JSON object: " in str(exc_info.value) +async def test_should_return_post_data_for_put_requests( + page: Page, server: Server +) -> None: + await page.goto(server.EMPTY_PAGE) + async with page.expect_request("**/*") as request_info: + await page.evaluate( + """({url}) => { + const request = new Request(url, { + method: 'PUT', + body: JSON.stringify({ value: 42 }), + }); + return fetch(request); + }""", + {"url": server.PREFIX + "/title.html"}, + ) + request = await request_info.value + assert request.post_data_json == {"value": 42} + + +async def test_should_parse_the_json_post_data_when_content_type_header_is_missing( + page: Page, server: Server +) -> None: + await page.goto(server.EMPTY_PAGE) + async with page.expect_request("**/*") as request_info: + await page.evaluate( + """({url}) => { + const request = new Request(url, { + method: 'POST', + body: JSON.stringify({ value: 42 }), + }); + request.headers.delete('content-type'); + return fetch(request); + }""", + {"url": server.PREFIX + "/title.html"}, + ) + request = await request_info.value + assert "content-type" not in request.headers + assert request.post_data_json == {"value": 42} + + async def test_should_work_with_binary_post_data(page: Page, server: Server) -> None: await page.goto(server.EMPTY_PAGE) server.set_route("/post", lambda req: req.finish())