Skip to content
Merged
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
4 changes: 2 additions & 2 deletions playwright/_impl/_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions tests/async/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,46 @@ async def test_should_throw_on_invalid_json_in_post_data(
assert "POST data is not a valid JSON object: <not a json>" 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())
Expand Down
Loading