From 1210ff7703680aaba381e10e7e9c38c513b3d6a5 Mon Sep 17 00:00:00 2001 From: "microsoft-playwright-automation[bot]" <203992400+microsoft-playwright-automation[bot]@users.noreply.github.com> Date: Thu, 5 Nov 2026 22:53:34 +0000 Subject: [PATCH] docs(api): clarify JSON string request bodies Fixes: https://github.com/microsoft/playwright/issues/43937 --- docs/src/api/class-apirequestcontext.md | 35 ++++++-- docs/src/api/params.md | 20 ++++- docs/src/release-notes-js.md | 2 + packages/playwright-client/types/types.d.ts | 91 ++++++++++++++++----- packages/playwright-core/types/types.d.ts | 91 ++++++++++++++++----- 5 files changed, 188 insertions(+), 51 deletions(-) diff --git a/docs/src/api/class-apirequestcontext.md b/docs/src/api/class-apirequestcontext.md index 440526be01119..1687c742598de 100644 --- a/docs/src/api/class-apirequestcontext.md +++ b/docs/src/api/class-apirequestcontext.md @@ -272,7 +272,10 @@ context cookies from the response. The method will automatically follow redirect ### option: APIRequestContext.delete.headers = %%-js-python-csharp-fetch-option-headers-%% * since: v1.16 -### option: APIRequestContext.delete.data = %%-js-python-csharp-fetch-option-data-%% +### option: APIRequestContext.delete.data = %%-js-fetch-option-data-%% +* since: v1.17 + +### option: APIRequestContext.delete.data = %%-python-csharp-fetch-option-data-%% * since: v1.17 ### option: APIRequestContext.delete.form = %%-js-fetch-option-form-%% @@ -451,7 +454,10 @@ If set changes the fetch method (e.g. [PUT](https://developer.mozilla.org/en-US/ ### option: APIRequestContext.fetch.headers = %%-js-python-csharp-fetch-option-headers-%% * since: v1.16 -### option: APIRequestContext.fetch.data = %%-js-python-csharp-fetch-option-data-%% +### option: APIRequestContext.fetch.data = %%-js-fetch-option-data-%% +* since: v1.16 + +### option: APIRequestContext.fetch.data = %%-python-csharp-fetch-option-data-%% * since: v1.16 ### option: APIRequestContext.fetch.form = %%-js-fetch-option-form-%% @@ -566,7 +572,10 @@ await request.GetAsync("https://example.com/api/getText", new() { Params = query ### option: APIRequestContext.get.headers = %%-js-python-csharp-fetch-option-headers-%% * since: v1.16 -### option: APIRequestContext.get.data = %%-js-python-csharp-fetch-option-data-%% +### option: APIRequestContext.get.data = %%-js-fetch-option-data-%% +* since: v1.26 + +### option: APIRequestContext.get.data = %%-python-csharp-fetch-option-data-%% * since: v1.26 ### option: APIRequestContext.get.form = %%-js-fetch-option-form-%% @@ -633,7 +642,10 @@ context cookies from the response. The method will automatically follow redirect ### option: APIRequestContext.head.headers = %%-js-python-csharp-fetch-option-headers-%% * since: v1.16 -### option: APIRequestContext.head.data = %%-js-python-csharp-fetch-option-data-%% +### option: APIRequestContext.head.data = %%-js-fetch-option-data-%% +* since: v1.26 + +### option: APIRequestContext.head.data = %%-python-csharp-fetch-option-data-%% * since: v1.26 ### option: APIRequestContext.head.form = %%-python-fetch-option-form-%% @@ -700,7 +712,10 @@ context cookies from the response. The method will automatically follow redirect ### option: APIRequestContext.patch.headers = %%-js-python-csharp-fetch-option-headers-%% * since: v1.16 -### option: APIRequestContext.patch.data = %%-js-python-csharp-fetch-option-data-%% +### option: APIRequestContext.patch.data = %%-js-fetch-option-data-%% +* since: v1.16 + +### option: APIRequestContext.patch.data = %%-python-csharp-fetch-option-data-%% * since: v1.16 ### option: APIRequestContext.patch.form = %%-js-fetch-option-form-%% @@ -888,7 +903,10 @@ await request.PostAsync("https://example.com/api/uploadScript", new() { Multipar ### option: APIRequestContext.post.headers = %%-js-python-csharp-fetch-option-headers-%% * since: v1.16 -### option: APIRequestContext.post.data = %%-js-python-csharp-fetch-option-data-%% +### option: APIRequestContext.post.data = %%-js-fetch-option-data-%% +* since: v1.16 + +### option: APIRequestContext.post.data = %%-python-csharp-fetch-option-data-%% * since: v1.16 ### option: APIRequestContext.post.form = %%-js-fetch-option-form-%% @@ -955,7 +973,10 @@ context cookies from the response. The method will automatically follow redirect ### option: APIRequestContext.put.headers = %%-js-python-csharp-fetch-option-headers-%% * since: v1.16 -### option: APIRequestContext.put.data = %%-js-python-csharp-fetch-option-data-%% +### option: APIRequestContext.put.data = %%-js-fetch-option-data-%% +* since: v1.16 + +### option: APIRequestContext.put.data = %%-python-csharp-fetch-option-data-%% * since: v1.16 ### option: APIRequestContext.put.form = %%-python-fetch-option-form-%% diff --git a/docs/src/api/params.md b/docs/src/api/params.md index 749a50eebd0f8..964c3a00a4d07 100644 --- a/docs/src/api/params.md +++ b/docs/src/api/params.md @@ -579,8 +579,24 @@ unless explicitly provided. File values can be passed as file-like object contai An instance of [FormData] can be created via [`method: APIRequestContext.createFormData`]. -## js-python-csharp-fetch-option-data -* langs: js, python, csharp +## js-fetch-option-data +* langs: js +- `data` <[string]|[Buffer]|[Serializable]> + +Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be serialized to JSON +and the `content-type` header will be set to `application/json` if not explicitly set. + +If the data parameter is a string and the explicitly set `content-type` is `application/json` (case-insensitive, +with optional parameters such as `charset=utf-8`), the string is sent as is if it parses as JSON. +Otherwise, it is JSON-encoded as a string, including quotes and escaping. Strings with other content types +are sent as is. + +Pass a [Buffer], such as `Buffer.from(body, 'utf8')`, to send exact bytes without JSON encoding, +for example to send deliberately malformed JSON or a body used to compute a signature. +For strings and buffers, the `content-type` header defaults to `application/octet-stream` if not explicitly set. + +## python-csharp-fetch-option-data +* langs: python, csharp - `data` <[string]|[Buffer]|[Serializable]> Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string diff --git a/docs/src/release-notes-js.md b/docs/src/release-notes-js.md index c74cc2e3f2fee..8b40fe9ec4f14 100644 --- a/docs/src/release-notes-js.md +++ b/docs/src/release-notes-js.md @@ -92,6 +92,8 @@ export default defineConfig({ - String and `URLSearchParams` values of the `params` option in [APIRequestContext] requests are now appended to the query that the URL already has, instead of replacing it. +- [APIRequestContext] now matches `application/json` content types case-insensitively and with parameters, such as `application/json;charset=UTF-8`, when serializing string `data`. Previously, only the exact value `application/json` triggered JSON encoding. With these content types, a string that does not parse as JSON is now JSON-encoded, including quotes and escaping, instead of being sent as is. This changes malformed JSON bodies and can invalidate signatures computed over the original string. Pass `data: Buffer.from(body, 'utf8')` to send exact bytes without JSON encoding. Strings that already parse as JSON and `Buffer` data are unchanged. + - `.css`, `.less` and `.scss` imports in test files now load as empty modules instead of being removed, so the imported file must exist. - The blob reporter again deletes the content of its output directory before it writes the report. Playwright 1.64 kept reports with other names. diff --git a/packages/playwright-client/types/types.d.ts b/packages/playwright-client/types/types.d.ts index f77ad4a3dc002..f7f1a6c0f6566 100644 --- a/packages/playwright-client/types/types.d.ts +++ b/packages/playwright-client/types/types.d.ts @@ -19879,9 +19879,16 @@ export interface APIRequestContext { */ delete(url: string, options?: { /** - * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string - * and `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` - * header will be set to `application/octet-stream` if not explicitly set. + * Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be + * serialized to JSON and the `content-type` header will be set to `application/json` if not explicitly set. + * + * If the data parameter is a string and the explicitly set `content-type` is `application/json` (case-insensitive, + * with optional parameters such as `charset=utf-8`), the string is sent as is if it parses as JSON. Otherwise, it is + * JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is. + * + * Pass a [Buffer], such as `Buffer.from(body, 'utf8')`, to send exact bytes without JSON encoding, for example to + * send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the `content-type` + * header defaults to `application/octet-stream` if not explicitly set. */ data?: string|Buffer|Serializable; @@ -20021,9 +20028,16 @@ export interface APIRequestContext { */ fetch(urlOrRequest: string|Request, options?: { /** - * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string - * and `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` - * header will be set to `application/octet-stream` if not explicitly set. + * Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be + * serialized to JSON and the `content-type` header will be set to `application/json` if not explicitly set. + * + * If the data parameter is a string and the explicitly set `content-type` is `application/json` (case-insensitive, + * with optional parameters such as `charset=utf-8`), the string is sent as is if it parses as JSON. Otherwise, it is + * JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is. + * + * Pass a [Buffer], such as `Buffer.from(body, 'utf8')`, to send exact bytes without JSON encoding, for example to + * send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the `content-type` + * header defaults to `application/octet-stream` if not explicitly set. */ data?: string|Buffer|Serializable; @@ -20150,9 +20164,16 @@ export interface APIRequestContext { */ get(url: string, options?: { /** - * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string - * and `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` - * header will be set to `application/octet-stream` if not explicitly set. + * Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be + * serialized to JSON and the `content-type` header will be set to `application/json` if not explicitly set. + * + * If the data parameter is a string and the explicitly set `content-type` is `application/json` (case-insensitive, + * with optional parameters such as `charset=utf-8`), the string is sent as is if it parses as JSON. Otherwise, it is + * JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is. + * + * Pass a [Buffer], such as `Buffer.from(body, 'utf8')`, to send exact bytes without JSON encoding, for example to + * send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the `content-type` + * header defaults to `application/octet-stream` if not explicitly set. */ data?: string|Buffer|Serializable; @@ -20247,9 +20268,16 @@ export interface APIRequestContext { */ head(url: string, options?: { /** - * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string - * and `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` - * header will be set to `application/octet-stream` if not explicitly set. + * Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be + * serialized to JSON and the `content-type` header will be set to `application/json` if not explicitly set. + * + * If the data parameter is a string and the explicitly set `content-type` is `application/json` (case-insensitive, + * with optional parameters such as `charset=utf-8`), the string is sent as is if it parses as JSON. Otherwise, it is + * JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is. + * + * Pass a [Buffer], such as `Buffer.from(body, 'utf8')`, to send exact bytes without JSON encoding, for example to + * send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the `content-type` + * header defaults to `application/octet-stream` if not explicitly set. */ data?: string|Buffer|Serializable; @@ -20344,9 +20372,16 @@ export interface APIRequestContext { */ patch(url: string, options?: { /** - * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string - * and `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` - * header will be set to `application/octet-stream` if not explicitly set. + * Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be + * serialized to JSON and the `content-type` header will be set to `application/json` if not explicitly set. + * + * If the data parameter is a string and the explicitly set `content-type` is `application/json` (case-insensitive, + * with optional parameters such as `charset=utf-8`), the string is sent as is if it parses as JSON. Otherwise, it is + * JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is. + * + * Pass a [Buffer], such as `Buffer.from(body, 'utf8')`, to send exact bytes without JSON encoding, for example to + * send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the `content-type` + * header defaults to `application/octet-stream` if not explicitly set. */ data?: string|Buffer|Serializable; @@ -20483,9 +20518,16 @@ export interface APIRequestContext { */ post(url: string, options?: { /** - * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string - * and `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` - * header will be set to `application/octet-stream` if not explicitly set. + * Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be + * serialized to JSON and the `content-type` header will be set to `application/json` if not explicitly set. + * + * If the data parameter is a string and the explicitly set `content-type` is `application/json` (case-insensitive, + * with optional parameters such as `charset=utf-8`), the string is sent as is if it parses as JSON. Otherwise, it is + * JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is. + * + * Pass a [Buffer], such as `Buffer.from(body, 'utf8')`, to send exact bytes without JSON encoding, for example to + * send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the `content-type` + * header defaults to `application/octet-stream` if not explicitly set. */ data?: string|Buffer|Serializable; @@ -20580,9 +20622,16 @@ export interface APIRequestContext { */ put(url: string, options?: { /** - * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string - * and `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` - * header will be set to `application/octet-stream` if not explicitly set. + * Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be + * serialized to JSON and the `content-type` header will be set to `application/json` if not explicitly set. + * + * If the data parameter is a string and the explicitly set `content-type` is `application/json` (case-insensitive, + * with optional parameters such as `charset=utf-8`), the string is sent as is if it parses as JSON. Otherwise, it is + * JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is. + * + * Pass a [Buffer], such as `Buffer.from(body, 'utf8')`, to send exact bytes without JSON encoding, for example to + * send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the `content-type` + * header defaults to `application/octet-stream` if not explicitly set. */ data?: string|Buffer|Serializable; diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts index f77ad4a3dc002..f7f1a6c0f6566 100644 --- a/packages/playwright-core/types/types.d.ts +++ b/packages/playwright-core/types/types.d.ts @@ -19879,9 +19879,16 @@ export interface APIRequestContext { */ delete(url: string, options?: { /** - * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string - * and `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` - * header will be set to `application/octet-stream` if not explicitly set. + * Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be + * serialized to JSON and the `content-type` header will be set to `application/json` if not explicitly set. + * + * If the data parameter is a string and the explicitly set `content-type` is `application/json` (case-insensitive, + * with optional parameters such as `charset=utf-8`), the string is sent as is if it parses as JSON. Otherwise, it is + * JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is. + * + * Pass a [Buffer], such as `Buffer.from(body, 'utf8')`, to send exact bytes without JSON encoding, for example to + * send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the `content-type` + * header defaults to `application/octet-stream` if not explicitly set. */ data?: string|Buffer|Serializable; @@ -20021,9 +20028,16 @@ export interface APIRequestContext { */ fetch(urlOrRequest: string|Request, options?: { /** - * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string - * and `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` - * header will be set to `application/octet-stream` if not explicitly set. + * Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be + * serialized to JSON and the `content-type` header will be set to `application/json` if not explicitly set. + * + * If the data parameter is a string and the explicitly set `content-type` is `application/json` (case-insensitive, + * with optional parameters such as `charset=utf-8`), the string is sent as is if it parses as JSON. Otherwise, it is + * JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is. + * + * Pass a [Buffer], such as `Buffer.from(body, 'utf8')`, to send exact bytes without JSON encoding, for example to + * send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the `content-type` + * header defaults to `application/octet-stream` if not explicitly set. */ data?: string|Buffer|Serializable; @@ -20150,9 +20164,16 @@ export interface APIRequestContext { */ get(url: string, options?: { /** - * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string - * and `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` - * header will be set to `application/octet-stream` if not explicitly set. + * Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be + * serialized to JSON and the `content-type` header will be set to `application/json` if not explicitly set. + * + * If the data parameter is a string and the explicitly set `content-type` is `application/json` (case-insensitive, + * with optional parameters such as `charset=utf-8`), the string is sent as is if it parses as JSON. Otherwise, it is + * JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is. + * + * Pass a [Buffer], such as `Buffer.from(body, 'utf8')`, to send exact bytes without JSON encoding, for example to + * send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the `content-type` + * header defaults to `application/octet-stream` if not explicitly set. */ data?: string|Buffer|Serializable; @@ -20247,9 +20268,16 @@ export interface APIRequestContext { */ head(url: string, options?: { /** - * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string - * and `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` - * header will be set to `application/octet-stream` if not explicitly set. + * Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be + * serialized to JSON and the `content-type` header will be set to `application/json` if not explicitly set. + * + * If the data parameter is a string and the explicitly set `content-type` is `application/json` (case-insensitive, + * with optional parameters such as `charset=utf-8`), the string is sent as is if it parses as JSON. Otherwise, it is + * JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is. + * + * Pass a [Buffer], such as `Buffer.from(body, 'utf8')`, to send exact bytes without JSON encoding, for example to + * send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the `content-type` + * header defaults to `application/octet-stream` if not explicitly set. */ data?: string|Buffer|Serializable; @@ -20344,9 +20372,16 @@ export interface APIRequestContext { */ patch(url: string, options?: { /** - * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string - * and `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` - * header will be set to `application/octet-stream` if not explicitly set. + * Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be + * serialized to JSON and the `content-type` header will be set to `application/json` if not explicitly set. + * + * If the data parameter is a string and the explicitly set `content-type` is `application/json` (case-insensitive, + * with optional parameters such as `charset=utf-8`), the string is sent as is if it parses as JSON. Otherwise, it is + * JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is. + * + * Pass a [Buffer], such as `Buffer.from(body, 'utf8')`, to send exact bytes without JSON encoding, for example to + * send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the `content-type` + * header defaults to `application/octet-stream` if not explicitly set. */ data?: string|Buffer|Serializable; @@ -20483,9 +20518,16 @@ export interface APIRequestContext { */ post(url: string, options?: { /** - * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string - * and `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` - * header will be set to `application/octet-stream` if not explicitly set. + * Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be + * serialized to JSON and the `content-type` header will be set to `application/json` if not explicitly set. + * + * If the data parameter is a string and the explicitly set `content-type` is `application/json` (case-insensitive, + * with optional parameters such as `charset=utf-8`), the string is sent as is if it parses as JSON. Otherwise, it is + * JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is. + * + * Pass a [Buffer], such as `Buffer.from(body, 'utf8')`, to send exact bytes without JSON encoding, for example to + * send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the `content-type` + * header defaults to `application/octet-stream` if not explicitly set. */ data?: string|Buffer|Serializable; @@ -20580,9 +20622,16 @@ export interface APIRequestContext { */ put(url: string, options?: { /** - * Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string - * and `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` - * header will be set to `application/octet-stream` if not explicitly set. + * Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be + * serialized to JSON and the `content-type` header will be set to `application/json` if not explicitly set. + * + * If the data parameter is a string and the explicitly set `content-type` is `application/json` (case-insensitive, + * with optional parameters such as `charset=utf-8`), the string is sent as is if it parses as JSON. Otherwise, it is + * JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is. + * + * Pass a [Buffer], such as `Buffer.from(body, 'utf8')`, to send exact bytes without JSON encoding, for example to + * send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the `content-type` + * header defaults to `application/octet-stream` if not explicitly set. */ data?: string|Buffer|Serializable;