Skip to content

Commit 95db482

Browse files
Skn0ttCopilot
andauthored
fix: handle missing content-type header in Request.post_data_json (#3095)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e4892fa commit 95db482

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

playwright/_impl/_network.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ def post_data_json(self) -> Optional[Any]:
220220
post_data = self.post_data
221221
if not post_data:
222222
return None
223-
content_type = self.headers["content-type"]
224-
if "application/x-www-form-urlencoded" in content_type:
223+
content_type = self.headers.get("content-type")
224+
if content_type and "application/x-www-form-urlencoded" in content_type:
225225
return dict(parse.parse_qsl(post_data))
226226
try:
227227
return json.loads(post_data)

tests/async/test_network.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,46 @@ async def test_should_throw_on_invalid_json_in_post_data(
468468
assert "POST data is not a valid JSON object: <not a json>" in str(exc_info.value)
469469

470470

471+
async def test_should_return_post_data_for_put_requests(
472+
page: Page, server: Server
473+
) -> None:
474+
await page.goto(server.EMPTY_PAGE)
475+
async with page.expect_request("**/*") as request_info:
476+
await page.evaluate(
477+
"""({url}) => {
478+
const request = new Request(url, {
479+
method: 'PUT',
480+
body: JSON.stringify({ value: 42 }),
481+
});
482+
return fetch(request);
483+
}""",
484+
{"url": server.PREFIX + "/title.html"},
485+
)
486+
request = await request_info.value
487+
assert request.post_data_json == {"value": 42}
488+
489+
490+
async def test_should_parse_the_json_post_data_when_content_type_header_is_missing(
491+
page: Page, server: Server
492+
) -> None:
493+
await page.goto(server.EMPTY_PAGE)
494+
async with page.expect_request("**/*") as request_info:
495+
await page.evaluate(
496+
"""({url}) => {
497+
const request = new Request(url, {
498+
method: 'POST',
499+
body: JSON.stringify({ value: 42 }),
500+
});
501+
request.headers.delete('content-type');
502+
return fetch(request);
503+
}""",
504+
{"url": server.PREFIX + "/title.html"},
505+
)
506+
request = await request_info.value
507+
assert "content-type" not in request.headers
508+
assert request.post_data_json == {"value": 42}
509+
510+
471511
async def test_should_work_with_binary_post_data(page: Page, server: Server) -> None:
472512
await page.goto(server.EMPTY_PAGE)
473513
server.set_route("/post", lambda req: req.finish())

0 commit comments

Comments
 (0)