Skip to main content

RequestOptions

RequestOptions 允许创建表单数据,通过 APIRequestContext 发送。Playwright会自动确定请求的内容类型。

🌐 The RequestOptions allows to create form data to be sent via APIRequestContext. Playwright will automatically determine content type of the request.

context.request().post(
"https://example.com/submit",
RequestOptions.create()
.setQueryParam("page", 1)
.setData("My data"));

上传 HTML 表单数据

FormData 类可用于向服务器发送表单,默认情况下请求将使用 application/x-www-form-urlencoded 编码:

context.request().post("https://example.com/signup", RequestOptions.create().setForm(
FormData.create()
.set("firstName", "John")
.set("lastName", "Doe")));

你也可以将文件作为 HTML 表单的字段发送。数据将使用 multipart/form-data 编码:

🌐 You can also send files as fields of an html form. The data will be encoded using multipart/form-data:

Path path = Paths.get("members.csv");
APIResponse response = context.request().post("https://example.com/upload_members",
RequestOptions.create().setMultipart(FormData.create().set("membersList", path)));

或者,你可以手动构建文件负载:

🌐 Alternatively, you can build the file payload manually:

FilePayload filePayload = new FilePayload("members.csv", "text/csv",
"Alice, 33\nJohn, 35\n".getBytes(StandardCharsets.UTF_8));
APIResponse response = context.request().post("https://example.com/upload_members",
RequestOptions.create().setMultipart(FormData.create().set("membersList", filePayload)));

方法

🌐 Methods

create

Added in: v1.18 requestOptions.create

创建 RequestOptions 的新实例。

🌐 Creates new instance of RequestOptions.

用法

RequestOptions.create();

返回


setData

Added in: v1.18 requestOptions.setData

设置请求的 POST 数据。

🌐 Sets the request's post data.

用法

RequestOptions.setData(data);

参数

  • data String | byte[] | Object#

    允许设置请求的 POST 数据。如果 data 参数是一个对象,它将被序列化为 JSON 字符串,并且如果未显式设置,content-type 头将被设置为 application/json。否则,如果未显式设置,content-type 头将被设置为 application/octet-stream

返回


setFailOnStatusCode

Added in: v1.18 requestOptions.setFailOnStatusCode

用法

RequestOptions.setFailOnStatusCode(failOnStatusCode);

参数

  • failOnStatusCode boolean#

    是否对除了 2xx 和 3xx 之外的响应代码抛出异常。默认情况下,响应对象会返回所有状态代码。

返回


setForm

Added in: v1.18 requestOptions.setForm

提供 FormData 对象,该对象将使用 application/x-www-form-urlencoded 编码序列化为 HTML 表单并作为此请求的正文发送。如果指定了此参数,除非明确提供,否则 content-type 头将设置为 application/x-www-form-urlencoded

🌐 Provides FormData 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.

用法

RequestOptions.setForm(form);

参数

  • form FormData#

    表单数据将使用 application/x-www-form-urlencoded 编码序列化为 HTML 表单,并作为此请求体发送。

返回


setHeader

Added in: v1.18 requestOptions.setHeader

为请求设置 HTTP 头。该头将适用于被获取的请求以及由其发起的任何重定向。

🌐 Sets an HTTP header to the request. This header will apply to the fetched request as well as any redirects initiated by it.

用法

RequestOptions.setHeader(name, value);

参数

返回


setIgnoreHTTPSErrors

Added in: v1.18 requestOptions.setIgnoreHTTPSErrors

用法

RequestOptions.setIgnoreHTTPSErrors(ignoreHTTPSErrors);

参数

  • ignoreHTTPSErrors boolean#

    发送网络请求时是否忽略 HTTPS 错误。

返回


setMaxRedirects

Added in: v1.26 requestOptions.setMaxRedirects

用法

RequestOptions.setMaxRedirects(maxRedirects);

参数

  • maxRedirects int#

    自动跟随的请求重定向的最大次数。如果超过该次数,将抛出错误。默认值为 20。传入 0 可不跟随重定向。

返回


setMaxRetries

Added in: v1.46 requestOptions.setMaxRetries

用法

RequestOptions.setMaxRetries(maxRetries);

参数

  • maxRetries int#

    网络错误应重试的最大次数。目前仅重试 ECONNRESET 错误。不基于 HTTP 响应码进行重试。如果超过限制,将抛出错误。默认值为 0 - 不重试。

返回


setMethod

Added in: v1.18 requestOptions.setMethod

更改请求方法(例如 PUTPOST)。

🌐 Changes the request method (e.g. PUT or POST).

用法

RequestOptions.setMethod(method);

参数

返回


setMultipart

Added in: v1.18 requestOptions.setMultipart

提供 FormData 对象,该对象将使用 multipart/form-data 编码序列化为 HTML 表单并作为此请求的正文发送。如果指定了此参数,除非明确提供,否则 content-type 头将设置为 multipart/form-data

🌐 Provides FormData object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided.

用法

RequestOptions.setMultipart(form);

参数

  • form FormData#

    表单数据将使用 multipart/form-data 编码序列化为 HTML 表单,并作为此请求体发送。

返回


setQueryParam

Added in: v1.18 requestOptions.setQueryParam

向请求 URL 添加查询参数。

🌐 Adds a query parameter to the request URL.

用法

RequestOptions.setQueryParam(name, value);

参数

返回


setTimeout

Added in: v1.18 requestOptions.setTimeout

以毫秒为单位设置请求超时。默认值为 30000(30 秒)。传入 0 可禁用超时。

🌐 Sets request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

用法

RequestOptions.setTimeout(timeout);

参数

  • timeout double#

    请求超时(以毫秒为单位)。

返回