diff --git a/playwright/_impl/_fetch.py b/playwright/_impl/_fetch.py index 3a71a5ff5..da17c169c 100644 --- a/playwright/_impl/_fetch.py +++ b/playwright/_impl/_fetch.py @@ -338,7 +338,7 @@ async def _inner_fetch( form_data: Optional[List[NameValue]] = None multipart_data: Optional[List[FormField]] = None post_data_buffer: Optional[bytes] = None - if data: + if data is not None: if isinstance(data, str): if is_json_content_type(serialized_headers): json_data = data if is_json_parsable(data) else json.dumps(data) diff --git a/tests/async/test_fetch_global.py b/tests/async/test_fetch_global.py index eda3145ee..82ecf38ec 100644 --- a/tests/async/test_fetch_global.py +++ b/tests/async/test_fetch_global.py @@ -448,12 +448,18 @@ async def test_should_throw_an_error_when_max_redirects_is_less_than_0( assert "'max_redirects' must be greater than or equal to '0'" in str(exc_info) -async def test_should_serialize_null_values_in_json( +async def test_should_serialize_request_data( playwright: Playwright, server: Server ) -> None: request = await playwright.request.new_context() server.set_route("/echo", lambda req: (req.write(req.post_body), req.finish())) - response = await request.post(server.PREFIX + "/echo", data={"foo": None}) - assert response.status == 200 - assert await response.text() == '{"foo": null}' + for data, expected in [ + ({"foo": None}, '{"foo": null}'), + ([], "[]"), + ({}, "{}"), + ("", ""), + ]: + response = await request.post(server.PREFIX + "/echo", data=data) + assert response.status == 200 + assert await response.text() == expected await request.dispose()