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 表单数据
¥Uploading html form data
FormData 类可用于向服务器发送表单,默认情况下,请求将使用 application/x-www-form-urlencoded
编码:
¥FormData class can be used to send a form to the server, by default the request will use application/x-www-form-urlencoded
encoding:
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 的新实例。
¥Creates new instance of RequestOptions.
用法
¥Usage
RequestOptions.create();
返回
¥Returns
setData
Added in: v1.18设置请求的 POST 数据。
¥Sets the request's post data.
用法
¥Usage
RequestOptions.setData(data);
参数
¥Arguments
允许设置请求的发布数据。如果 data 参数是一个对象,它将被序列化为 json 字符串,如果没有显式设置,content-type
header 将被设置为 application/json
。否则,如果未显式设置,content-type
标头将设置为 application/octet-stream
。
¥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.
返回
¥Returns
setFailOnStatusCode
Added in: v1.18用法
¥Usage
RequestOptions.setFailOnStatusCode(failOnStatusCode);
参数
¥Arguments
是否抛出除 2xx 和 3xx 之外的响应代码。默认情况下,为所有状态代码返回响应对象。
¥Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
返回
¥Returns
setForm
Added in: v1.18提供 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.
用法
¥Usage
RequestOptions.setForm(form);
参数
¥Arguments
表单数据将使用 application/x-www-form-urlencoded
编码序列化为 HTML 表单,并作为请求主体发送。
¥Form data to be serialized as html form using application/x-www-form-urlencoded
encoding and sent as this request body.
返回
¥Returns
setHeader
Added in: v1.18设置请求的 HTTP 标头。此标头将应用于获取的请求以及由其发起的任何重定向。
¥Sets an HTTP header to the request. This header will apply to the fetched request as well as any redirects initiated by it.
用法
¥Usage
RequestOptions.setHeader(name, value);
参数
¥Arguments
标头名称。
¥Header name.
标头值。
¥Header value.
返回
¥Returns
setIgnoreHTTPSErrors
Added in: v1.18用法
¥Usage
RequestOptions.setIgnoreHTTPSErrors(ignoreHTTPSErrors);
参数
¥Arguments
发送网络请求时是否忽略 HTTPS 错误。
¥Whether to ignore HTTPS errors when sending network requests.
返回
¥Returns
setMaxRedirects
Added in: v1.26用法
¥Usage
RequestOptions.setMaxRedirects(maxRedirects);
参数
¥Arguments
自动遵循的请求重定向的最大数量。如果超过数量将会抛出错误。默认为 20
。通过 0
不遵循重定向。
¥Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20
. Pass 0
to not follow redirects.
返回
¥Returns
setMaxRetries
Added in: v1.46用法
¥Usage
RequestOptions.setMaxRetries(maxRetries);
参数
¥Arguments
应重试网络错误的最大次数。当前仅重试 ECONNRESET
错误。不根据 HTTP 响应代码重试。如果超出限制,将引发错误。默认为 0
- 不重试。
¥Maximum number of times network errors should be retried. Currently only ECONNRESET
error is retried. Does not retry based on HTTP response codes. An error will be thrown if the limit is exceeded. Defaults to 0
- no retries.
返回
¥Returns
setMethod
Added in: v1.18¥Changes the request method (e.g. PUT or POST).
用法
¥Usage
RequestOptions.setMethod(method);
参数
¥Arguments
请求方法,例如 POST。
¥Request method, e.g. POST.
返回
¥Returns
setMultipart
Added in: v1.18提供 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.
用法
¥Usage
RequestOptions.setMultipart(form);
参数
¥Arguments
表单数据将使用 multipart/form-data
编码序列化为 HTML 表单,并作为请求主体发送。
¥Form data to be serialized as html form using multipart/form-data
encoding and sent as this request body.
返回
¥Returns
setQueryParam
Added in: v1.18向请求 URL 添加查询参数。
¥Adds a query parameter to the request URL.
用法
¥Usage
RequestOptions.setQueryParam(name, value);
参数
¥Arguments
参数名称。
¥Parameter name.
参数值。
¥Parameter value.
返回
¥Returns
setTimeout
Added in: v1.18设置请求超时时间(以毫秒为单位)。默认为 30000
(30 秒)。通过 0
禁用超时。
¥Sets request timeout in milliseconds. Defaults to 30000
(30 seconds). Pass 0
to disable timeout.
用法
¥Usage
RequestOptions.setTimeout(timeout);
参数
¥Arguments
请求超时(以毫秒为单位)。
¥Request timeout in milliseconds.
返回
¥Returns