跳到主要内容

RequestOptions

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

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 编码

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

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

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)));

方法

创建

添加于: v1.18 requestOptions.create

创建 RequestOptions 的新实例。

用法

RequestOptions.create();

返回值


setData

添加于: v1.18 requestOptions.setData

设置请求的 POST 数据。

用法

RequestOptions.setData(data);

参数

  • data String | byte[] | Object#

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

返回值


setFailOnStatusCode

添加于: v1.18 requestOptions.setFailOnStatusCode

用法

RequestOptions.setFailOnStatusCode(failOnStatusCode);

参数

  • failOnStatusCode boolean#

    是否在 2xx 和 3xx 以外的响应代码上抛出错误。默认情况下,将为所有状态代码返回 response 对象。

返回值


setForm

添加于: v1.18 requestOptions.setForm

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

用法

RequestOptions.setForm(form);

参数

  • form FormData#

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

返回值


setHeader

添加于: v1.18 requestOptions.setHeader

为请求设置 HTTP 标头。此标头将应用于获取的请求以及由此发起的任何重定向。

用法

RequestOptions.setHeader(name, value);

参数

返回值


setIgnoreHTTPSErrors

添加于: v1.18 requestOptions.setIgnoreHTTPSErrors

用法

RequestOptions.setIgnoreHTTPSErrors(ignoreHTTPSErrors);

参数

  • ignoreHTTPSErrors boolean#

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

返回值


setMaxRedirects

添加于: v1.26 requestOptions.setMaxRedirects

用法

RequestOptions.setMaxRedirects(maxRedirects);

参数

  • maxRedirects int#

    将自动跟踪的最大请求重定向次数。如果超出此数量,将抛出错误。默认为 20。传递 0 以不跟踪重定向。

返回值


setMaxRetries

添加于: v1.46 requestOptions.setMaxRetries

用法

RequestOptions.setMaxRetries(maxRetries);

参数

  • maxRetries int#

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

返回值


setMethod

添加于: v1.18 requestOptions.setMethod

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

用法

RequestOptions.setMethod(method);

参数

返回值


setMultipart

添加于: v1.18 requestOptions.setMultipart

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

用法

RequestOptions.setMultipart(form);

参数

  • form FormData#

    要使用 multipart/form-data 编码序列化为 HTML 表单并作为此请求正文发送的表单数据。

返回值


setQueryParam

添加于: v1.18 requestOptions.setQueryParam

向请求 URL 添加查询参数。

用法

RequestOptions.setQueryParam(name, value);

参数

返回值


setTimeout

添加于: v1.18 requestOptions.setTimeout

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

用法

RequestOptions.setTimeout(timeout);

参数

  • timeout double#

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

返回值