Skip to content

Commit d83dc6e

Browse files
authored
fix(fetch): serialise empty array in 'data' as JSON (#2476)
1 parent c6cc4c9 commit d83dc6e

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

playwright/_impl/_fetch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ async def _inner_fetch(
338338
form_data: Optional[List[NameValue]] = None
339339
multipart_data: Optional[List[FormField]] = None
340340
post_data_buffer: Optional[bytes] = None
341-
if data:
341+
if data is not None:
342342
if isinstance(data, str):
343343
if is_json_content_type(serialized_headers):
344344
json_data = data if is_json_parsable(data) else json.dumps(data)

tests/async/test_fetch_global.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,18 @@ async def test_should_throw_an_error_when_max_redirects_is_less_than_0(
448448
assert "'max_redirects' must be greater than or equal to '0'" in str(exc_info)
449449

450450

451-
async def test_should_serialize_null_values_in_json(
451+
async def test_should_serialize_request_data(
452452
playwright: Playwright, server: Server
453453
) -> None:
454454
request = await playwright.request.new_context()
455455
server.set_route("/echo", lambda req: (req.write(req.post_body), req.finish()))
456-
response = await request.post(server.PREFIX + "/echo", data={"foo": None})
457-
assert response.status == 200
458-
assert await response.text() == '{"foo": null}'
456+
for data, expected in [
457+
({"foo": None}, '{"foo": null}'),
458+
([], "[]"),
459+
({}, "{}"),
460+
("", ""),
461+
]:
462+
response = await request.post(server.PREFIX + "/echo", data=data)
463+
assert response.status == 200
464+
assert await response.text() == expected
459465
await request.dispose()

0 commit comments

Comments
 (0)