RequestOptions
RequestOptions 允许创建表单数据通过 APIRequestContext 发送。Playwright 会自动确定请求的内容类型。
context.request().post(
"https://example.com/submit",
RequestOptions.create()
.setQueryParam("page", 1)
.setData("My data"));
Uploading html form data
FormData 类可用于向服务器发送表单,默认情况下请求将使用 application/x-www-form-urlencoded
编码:
context.request().post("https://example.com/signup", RequestOptions.create().setForm(
FormData.create()
.set("firstName", "John")
.set("lastName", "Doe")));
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)));
或者,您可以手动构建文件负载:
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)));
方法
创建
Added in: v1.18创建RequestOptions的新实例。
用法
RequestOptions.create();
返回
setData
Added in: v1.18设置请求的post数据。
用法
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(failOnStatusCode);
参数
返回
setForm
Added in: v1.18Provides 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 data to be serialized as html form using
application/x-www-form-urlencoded
encoding and sent as this request body.
返回
setHeader
Added in: v1.18设置一个HTTP请求头。该请求头将应用于获取的请求以及由其发起的任何重定向。
用法
RequestOptions.setHeader(name, value);
参数
返回
setIgnoreHTTPSErrors
Added in: v1.18用法
RequestOptions.setIgnoreHTTPSErrors(ignoreHTTPSErrors);
参数
返回
setMaxRedirects
Added in: v1.26用法
RequestOptions.setMaxRedirects(maxRedirects);
参数
返回
设置最大重试次数
Added in: v1.46用法
RequestOptions.setMaxRetries(maxRetries);
参数
返回
setMethod
Added in: v1.18用法
RequestOptions.setMethod(method);
参数
返回
setMultipart
Added in: v1.18Provides 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 data to be serialized as html form using
multipart/form-data
encoding and sent as this request body.
返回
setQueryParam
Added in: v1.18向请求URL添加一个查询参数。
用法
RequestOptions.setQueryParam(name, value);
参数
返回
setTimeout
Added in: v1.18设置请求超时时间,单位为毫秒。默认为30000
(30秒)。传入0
表示禁用超时。
用法
RequestOptions.setTimeout(timeout);
参数
返回