From 1c5a6ca1903df2a4930749b5bf971e002ee1f7ce Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Fri, 13 Sep 2024 08:41:01 +0200 Subject: [PATCH] feat(library): accept `FormData` in `fetch` --- docs/src/api/class-apirequestcontext.md | 35 ++++++++++++++++---- docs/src/api/params.md | 12 +++++-- packages/playwright-core/src/client/fetch.ts | 15 +++++++-- packages/playwright-core/types/types.d.ts | 14 ++++---- tests/library/browsercontext-fetch.spec.ts | 16 +++++++++ 5 files changed, 73 insertions(+), 19 deletions(-) diff --git a/docs/src/api/class-apirequestcontext.md b/docs/src/api/class-apirequestcontext.md index 1d3e72823542c..f56087d1bd070 100644 --- a/docs/src/api/class-apirequestcontext.md +++ b/docs/src/api/class-apirequestcontext.md @@ -159,7 +159,10 @@ context cookies from the response. The method will automatically follow redirect ### option: APIRequestContext.delete.data = %%-js-python-csharp-fetch-option-data-%% * since: v1.17 -### option: APIRequestContext.delete.form = %%-js-python-fetch-option-form-%% +### option: APIRequestContext.delete.form = %%-js-fetch-option-form-%% +* since: v1.17 + +### option: APIRequestContext.delete.form = %%-python-fetch-option-form-%% * since: v1.17 ### option: APIRequestContext.delete.form = %%-csharp-fetch-option-form-%% @@ -332,7 +335,10 @@ If set changes the fetch method (e.g. [PUT](https://developer.mozilla.org/en-US/ ### option: APIRequestContext.fetch.data = %%-js-python-csharp-fetch-option-data-%% * since: v1.16 -### option: APIRequestContext.fetch.form = %%-js-python-fetch-option-form-%% +### option: APIRequestContext.fetch.form = %%-js-fetch-option-form-%% +* since: v1.16 + +### option: APIRequestContext.fetch.form = %%-python-fetch-option-form-%% * since: v1.16 ### option: APIRequestContext.fetch.form = %%-csharp-fetch-option-form-%% @@ -442,7 +448,10 @@ await request.GetAsync("https://example.com/api/getText", new() { Params = query ### option: APIRequestContext.get.data = %%-js-python-csharp-fetch-option-data-%% * since: v1.26 -### option: APIRequestContext.get.form = %%-js-python-fetch-option-form-%% +### option: APIRequestContext.get.form = %%-js-fetch-option-form-%% +* since: v1.26 + +### option: APIRequestContext.get.form = %%-python-fetch-option-form-%% * since: v1.26 ### option: APIRequestContext.get.form = %%-csharp-fetch-option-form-%% @@ -504,7 +513,10 @@ context cookies from the response. The method will automatically follow redirect ### option: APIRequestContext.head.data = %%-js-python-csharp-fetch-option-data-%% * since: v1.26 -### option: APIRequestContext.head.form = %%-js-python-fetch-option-form-%% +### option: APIRequestContext.head.form = %%-python-fetch-option-form-%% +* since: v1.26 + +### option: APIRequestContext.head.form = %%-js-fetch-option-form-%% * since: v1.26 ### option: APIRequestContext.head.form = %%-csharp-fetch-option-form-%% @@ -566,7 +578,10 @@ context cookies from the response. The method will automatically follow redirect ### option: APIRequestContext.patch.data = %%-js-python-csharp-fetch-option-data-%% * since: v1.16 -### option: APIRequestContext.patch.form = %%-js-python-fetch-option-form-%% +### option: APIRequestContext.patch.form = %%-js-fetch-option-form-%% +* since: v1.16 + +### option: APIRequestContext.patch.form = %%-python-fetch-option-form-%% * since: v1.16 ### option: APIRequestContext.patch.form = %%-csharp-fetch-option-form-%% @@ -749,7 +764,10 @@ await request.PostAsync("https://example.com/api/uploadScript", new() { Multipar ### option: APIRequestContext.post.data = %%-js-python-csharp-fetch-option-data-%% * since: v1.16 -### option: APIRequestContext.post.form = %%-js-python-fetch-option-form-%% +### option: APIRequestContext.post.form = %%-js-fetch-option-form-%% +* since: v1.16 + +### option: APIRequestContext.post.form = %%-python-fetch-option-form-%% * since: v1.16 ### option: APIRequestContext.post.form = %%-csharp-fetch-option-form-%% @@ -811,7 +829,10 @@ context cookies from the response. The method will automatically follow redirect ### option: APIRequestContext.put.data = %%-js-python-csharp-fetch-option-data-%% * since: v1.16 -### option: APIRequestContext.put.form = %%-js-python-fetch-option-form-%% +### option: APIRequestContext.put.form = %%-python-fetch-option-form-%% +* since: v1.16 + +### option: APIRequestContext.put.form = %%-js-fetch-option-form-%% * since: v1.16 ### option: APIRequestContext.put.form = %%-csharp-fetch-option-form-%% diff --git a/docs/src/api/params.md b/docs/src/api/params.md index cbec1a5e25c79..608855ab2aee8 100644 --- a/docs/src/api/params.md +++ b/docs/src/api/params.md @@ -405,8 +405,16 @@ Request timeout in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to d Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes. -## js-python-fetch-option-form -* langs: js, python +## js-fetch-option-form +* langs: js +- `form` <[Object]<[string], [string]|[float]|[boolean]>|[FormData]> + +Provides an object that will be serialized as html form using `application/x-www-form-urlencoded` encoding and sent as +this request body. If this parameter is specified `content-type` header will be set to `application/x-www-form-urlencoded` +unless explicitly provided. + +## python-fetch-option-form +* langs: python - `form` <[Object]<[string], [string]|[float]|[boolean]>> Provides an object that will be serialized as html form using `application/x-www-form-urlencoded` encoding and sent as diff --git a/packages/playwright-core/src/client/fetch.ts b/packages/playwright-core/src/client/fetch.ts index 87c31579b5180..58928532ac9e4 100644 --- a/packages/playwright-core/src/client/fetch.ts +++ b/packages/playwright-core/src/client/fetch.ts @@ -36,8 +36,8 @@ export type FetchOptions = { method?: string, headers?: Headers, data?: string | Buffer | Serializable, - form?: { [key: string]: string|number|boolean; }; - multipart?: { [key: string]: string|number|boolean|fs.ReadStream|FilePayload; }; + form?: { [key: string]: string|number|boolean; } | FormData; + multipart?: { [key: string]: string|number|boolean|fs.ReadStream|FilePayload; } | FormData; timeout?: number, failOnStatusCode?: boolean, ignoreHTTPSErrors?: boolean, @@ -202,7 +202,16 @@ export class APIRequestContext extends ChannelOwner