Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(network): request.postData() returns null for empty string body override (#41882)

tsushanthf5fa967ff581
diff --git a/packages/playwright-core/src/client/network.ts b/packages/playwright-core/src/client/network.ts
index 4d36f5dd0a..82286526e8 100644
--- a/packages/playwright-core/src/client/network.ts
+++ b/packages/playwright-core/src/client/network.ts
@@ -137,7 +137,7 @@ export class Request extends ChannelOwner<channels.RequestChannel> implements ap
}
postData(): string | null {
- return (this._fallbackOverrides.postDataBuffer || this._initializer.postData)?.toString('utf-8') || null;
+ return (this._fallbackOverrides.postDataBuffer ?? this._initializer.postData)?.toString('utf-8') ?? null;
}
postDataBuffer(): Buffer | null {
diff --git a/tests/page/page-request-continue.spec.ts b/tests/page/page-request-continue.spec.ts
index 48f9458012..a6b4137912 100644
--- a/tests/page/page-request-continue.spec.ts
+++ b/tests/page/page-request-continue.spec.ts
@@ -1010,3 +1010,15 @@ it('should not forward Host header on cross-origin redirect', {
expect(firstHost).toBe(new URL(server.PREFIX).host);
expect(redirectedHost).toBe(new URL(server.CROSS_PROCESS_PREFIX).host);
});
+
+it('postData should return empty string when overriding body with empty string', async ({ page, server }) => {
+ await page.goto(server.EMPTY_PAGE);
+ await page.route('**/*', route => {
+ void route.continue({ postData: '' });
+ });
+ const [request] = await Promise.all([
+ page.waitForRequest('**'),
+ page.evaluate(({ url }) => fetch(url, { method: 'POST', body: 'original' }), { url: server.PREFIX + '/sleep.zzz' }),
+ ]);
+ expect(request.postData()).toBe('');
+});