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
44 changes: 34 additions & 10 deletions docs/src/api/class-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@ request is issued to a redirected url.

An object with all the request HTTP headers associated with this request. The header names are lower-cased.

## async method: Request.body
* since: v1.57
- returns: <[null]|[string]>

The request body, if present.

## async method: Request.bodyBuffer
* since: v1.57
- returns: <[null]|[Buffer]>

The request body in a binary form. Returns null if the request has no body.

## async method: Request.bodyJSON
* since: v1.57
* langs: js, python
- returns: <[null]|[Serializable]>

Returns the request body as a parsed JSON object. If the request `Content-Type` is `application/x-www-form-urlencoded`, this method returns a key/value object parsed from the form data. Otherwise, it parses the body as JSON.

## async method: Request.bodyJSON
* since: v1.57
* langs: csharp
- returns: <[null]|[JsonElement]>

Returns the request body as a parsed JSON object. If the request `Content-Type` is `application/x-www-form-urlencoded`, this method returns a key/value object parsed from the form data. Otherwise, it parses the body as JSON.

## method: Request.failure
* since: v1.8
- returns: <[null]|[string]>
Expand Down Expand Up @@ -149,35 +175,33 @@ Request's method (GET, POST, etc.)

## method: Request.postData
* since: v1.8
* discouraged: Use [`method: Request.body`] instead.
- returns: <[null]|[string]>

Request's post body, if any.
The request body, if present.

## method: Request.postDataBuffer
* since: v1.8
* discouraged: Use [`method: Request.bodyBuffer`] instead.
- returns: <[null]|[Buffer]>

Request's post body in a binary form, if any.
The request body in a binary form. Returns null if the request has no body.

## method: Request.postDataJSON
* since: v1.8
* langs: js, python
* discouraged: Use [`method: Request.bodyJSON`] instead.
- returns: <[null]|[Serializable]>

Returns parsed request's body for `form-urlencoded` and JSON as a fallback if any.

When the response is `application/x-www-form-urlencoded` then a key/value object of the values will be returned.
Otherwise it will be parsed as JSON.
Returns the request body as a parsed JSON object. If the request `Content-Type` is `application/x-www-form-urlencoded`, this method returns a key/value object parsed from the form data. Otherwise, it parses the body as JSON.

## method: Request.postDataJSON
* since: v1.12
* langs: csharp
* discouraged: Use [`method: Request.bodyJSON`] instead.
- returns: <[null]|[JsonElement]>

Returns parsed request's body for `form-urlencoded` and JSON as a fallback if any.

When the response is `application/x-www-form-urlencoded` then a key/value object of the values will be returned.
Otherwise it will be parsed as JSON.
Returns the request body as a parsed JSON object. If the request `Content-Type` is `application/x-www-form-urlencoded`, this method returns a key/value object parsed from the form data. Otherwise, it parses the body as JSON.

## method: Request.redirectedFrom
* since: v1.8
Expand Down
32 changes: 27 additions & 5 deletions packages/playwright-client/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20309,6 +20309,23 @@ export interface Request {
*/
allHeaders(): Promise<{ [key: string]: string; }>;

/**
* The request body, if present.
*/
body(): Promise<null|string>;

/**
* The request body in a binary form. Returns null if the request has no body.
*/
bodyBuffer(): Promise<null|Buffer>;

/**
* Returns the request body as a parsed JSON object. If the request `Content-Type` is
* `application/x-www-form-urlencoded`, this method returns a key/value object parsed from the form data. Otherwise,
* it parses the body as JSON.
*/
bodyJSON(): Promise<null|Serializable>;

/**
* The method returns `null` unless this request has failed, as reported by `requestfailed` event.
*
Expand Down Expand Up @@ -20406,20 +20423,25 @@ export interface Request {
method(): string;

/**
* Request's post body, if any.
* **NOTE** Use [request.body()](https://playwright.dev/docs/api/class-request#request-body) instead.
*
* The request body, if present.
*/
postData(): null|string;

/**
* Request's post body in a binary form, if any.
* **NOTE** Use [request.bodyBuffer()](https://playwright.dev/docs/api/class-request#request-body-buffer) instead.
*
* The request body in a binary form. Returns null if the request has no body.
*/
postDataBuffer(): null|Buffer;

/**
* Returns parsed request's body for `form-urlencoded` and JSON as a fallback if any.
* **NOTE** Use [request.bodyJSON()](https://playwright.dev/docs/api/class-request#request-body-json) instead.
*
* When the response is `application/x-www-form-urlencoded` then a key/value object of the values will be returned.
* Otherwise it will be parsed as JSON.
* Returns the request body as a parsed JSON object. If the request `Content-Type` is
* `application/x-www-form-urlencoded`, this method returns a key/value object parsed from the form data. Otherwise,
* it parses the body as JSON.
*/
postDataJSON(): null|Serializable;

Expand Down
16 changes: 16 additions & 0 deletions packages/playwright-core/src/client/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ export class Request extends ChannelOwner<channels.RequestChannel> implements ap
return this._fallbackOverrides.method || this._initializer.method;
}

async body(): Promise<string | null> {
return (this._fallbackOverrides.postDataBuffer || (await this._channel.body()).body)?.toString('utf-8') || null;
}

async bodyBuffer(): Promise<Buffer | null> {
return this._fallbackOverrides.postDataBuffer || (await this._channel.body()).body || null;
}

async bodyJSON(): Promise<Object | null> {
return this._postDataJSON(await this.body());
}

postData(): string | null {
return (this._fallbackOverrides.postDataBuffer || this._initializer.postData)?.toString('utf-8') || null;
}
Expand All @@ -144,6 +156,10 @@ export class Request extends ChannelOwner<channels.RequestChannel> implements ap

postDataJSON(): Object | null {
const postData = this.postData();
return this._postDataJSON(postData);
}

private _postDataJSON(postData: string | null): Object | null {
if (!postData)
return null;

Expand Down
4 changes: 4 additions & 0 deletions packages/playwright-core/src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,10 @@ scheme.RequestInitializer = tObject({
hasResponse: tBoolean,
});
scheme.RequestResponseEvent = tOptional(tObject({}));
scheme.RequestBodyParams = tOptional(tObject({}));
scheme.RequestBodyResult = tObject({
body: tOptional(tBinary),
});
scheme.RequestResponseParams = tOptional(tObject({}));
scheme.RequestResponseResult = tObject({
response: tOptional(tChannel(['Response'])),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export class RequestDispatcher extends Dispatcher<Request, channels.RequestChann
this.addObjectListener(Request.Events.Response, () => this._dispatchEvent('response', {}));
}

async body(params: channels.RequestBodyParams, progress: Progress): Promise<channels.RequestBodyResult> {
const postData = this._object.postDataBuffer();
return { body: postData === null ? undefined : postData };
}

async rawRequestHeaders(params: channels.RequestRawRequestHeadersParams, progress: Progress): Promise<channels.RequestRawRequestHeadersResult> {
return { headers: await progress.race(this._object.rawRequestHeaders()) };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export const methodMetainfo = new Map<string, { internal?: boolean, title?: stri
['ElementHandle.uncheck', { title: 'Uncheck', slowMo: true, snapshot: true, pausesBeforeInput: true, }],
['ElementHandle.waitForElementState', { title: 'Wait for state', snapshot: true, pausesBeforeAction: true, }],
['ElementHandle.waitForSelector', { title: 'Wait for selector', snapshot: true, }],
['Request.body', { title: 'Get request body', group: 'getter', }],
['Request.response', { internal: true, }],
['Request.rawRequestHeaders', { internal: true, }],
['Route.redirectNavigationRequest', { internal: true, }],
Expand Down
32 changes: 27 additions & 5 deletions packages/playwright-core/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20309,6 +20309,23 @@ export interface Request {
*/
allHeaders(): Promise<{ [key: string]: string; }>;

/**
* The request body, if present.
*/
body(): Promise<null|string>;

/**
* The request body in a binary form. Returns null if the request has no body.
*/
bodyBuffer(): Promise<null|Buffer>;

/**
* Returns the request body as a parsed JSON object. If the request `Content-Type` is
* `application/x-www-form-urlencoded`, this method returns a key/value object parsed from the form data. Otherwise,
* it parses the body as JSON.
*/
bodyJSON(): Promise<null|Serializable>;

/**
* The method returns `null` unless this request has failed, as reported by `requestfailed` event.
*
Expand Down Expand Up @@ -20406,20 +20423,25 @@ export interface Request {
method(): string;

/**
* Request's post body, if any.
* **NOTE** Use [request.body()](https://playwright.dev/docs/api/class-request#request-body) instead.
*
* The request body, if present.
*/
postData(): null|string;

/**
* Request's post body in a binary form, if any.
* **NOTE** Use [request.bodyBuffer()](https://playwright.dev/docs/api/class-request#request-body-buffer) instead.
*
* The request body in a binary form. Returns null if the request has no body.
*/
postDataBuffer(): null|Buffer;

/**
* Returns parsed request's body for `form-urlencoded` and JSON as a fallback if any.
* **NOTE** Use [request.bodyJSON()](https://playwright.dev/docs/api/class-request#request-body-json) instead.
*
* When the response is `application/x-www-form-urlencoded` then a key/value object of the values will be returned.
* Otherwise it will be parsed as JSON.
* Returns the request body as a parsed JSON object. If the request `Content-Type` is
* `application/x-www-form-urlencoded`, this method returns a key/value object parsed from the form data. Otherwise,
* it parses the body as JSON.
*/
postDataJSON(): null|Serializable;

Expand Down
6 changes: 6 additions & 0 deletions packages/protocol/src/channels.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3815,10 +3815,16 @@ export interface RequestEventTarget {
}
export interface RequestChannel extends RequestEventTarget, Channel {
_type_Request: boolean;
body(params?: RequestBodyParams, progress?: Progress): Promise<RequestBodyResult>;
response(params?: RequestResponseParams, progress?: Progress): Promise<RequestResponseResult>;
rawRequestHeaders(params?: RequestRawRequestHeadersParams, progress?: Progress): Promise<RequestRawRequestHeadersResult>;
}
export type RequestResponseEvent = {};
export type RequestBodyParams = {};
export type RequestBodyOptions = {};
export type RequestBodyResult = {
body?: Binary,
};
export type RequestResponseParams = {};
export type RequestResponseOptions = {};
export type RequestResponseResult = {
Expand Down
6 changes: 6 additions & 0 deletions packages/protocol/src/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3377,6 +3377,12 @@ Request:

commands:

body:
title: Get request body
group: getter
returns:
body: binary?

response:
internal: true
returns:
Expand Down
121 changes: 121 additions & 0 deletions tests/page/network-request-body.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { test as it, expect } from './pageTest';

it('should return correct request body buffer for utf-8 body', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
const value = 'baẞ';
const [request] = await Promise.all([
page.waitForRequest('**'),
page.evaluate(({ url, value }) => {
const request = new Request(url, {
method: 'POST',
body: JSON.stringify(value),
});
request.headers.set('content-type', 'application/json;charset=UTF-8');
return fetch(request);
}, { url: server.PREFIX + '/title.html', value })
]);
expect((await request.bodyBuffer()).equals(Buffer.from(JSON.stringify(value), 'utf-8'))).toBe(true);
expect(await request.bodyJSON()).toBe(value);
});

it('should return request body w/o content-type @smoke', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
const [request] = await Promise.all([
page.waitForRequest('**'),
page.evaluate(({ url }) => {
const request = new Request(url, {
method: 'POST',
body: JSON.stringify({ value: 42 }),
});
request.headers.set('content-type', '');
return fetch(request);
}, { url: server.PREFIX + '/title.html' })
]);
expect(await request.bodyJSON()).toEqual({ value: 42 });
});

it('should throw on invalid JSON in post data', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
const [request] = await Promise.all([
page.waitForRequest('**'),
page.evaluate(({ url }) => {
const request = new Request(url, {
method: 'POST',
body: '<not a json>',
});
return fetch(request);
}, { url: server.PREFIX + '/title.html' })
]);
let error;
try {
await request.bodyJSON();
} catch (e) {
error = e;
}
expect(error.message).toContain('POST data is not a valid JSON object: <not a json>');
});

it('should return body for PUT requests', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
const [request] = await Promise.all([
page.waitForRequest('**'),
page.evaluate(({ url }) => {
const request = new Request(url, {
method: 'PUT',
body: JSON.stringify({ value: 42 }),
});
return fetch(request);
}, { url: server.PREFIX + '/title.html' })
]);
expect(await request.bodyJSON()).toEqual({ value: 42 });
});

it('should get request body for file/blob', async ({ page, server, browserName }) => {
it.fail(browserName === 'webkit' || browserName === 'chromium');
await page.goto(server.EMPTY_PAGE);
const [request] = await Promise.all([
page.waitForRequest('**/*'),
page.evaluate(() => {
const file = new File(['file-contents'], 'filename.txt');

void fetch('/data', {
method: 'POST',
headers: {
'content-type': 'application/octet-stream'
},
body: file
});
})
]);
expect(await request.body()).toBe('file-contents');
});

it('should get request body for navigator.sendBeacon api calls', async ({ page, server, browserName }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/12231' });
it.fail(browserName === 'chromium', 'body is empty');
it.fail(browserName === 'webkit', 'body is empty');
await page.goto(server.EMPTY_PAGE);
const [request] = await Promise.all([
page.waitForRequest('**/*'),
page.evaluate(() => navigator.sendBeacon(window.location.origin + '/api/foo', new Blob([JSON.stringify({ foo: 'bar' })])))
]);
expect(request.method()).toBe('POST');
expect(request.url()).toBe(server.PREFIX + '/api/foo');
expect(await request.bodyJSON()).toStrictEqual({ foo: 'bar' });
});
Loading
Loading