跳到主要内容

FormData

FormData 用于创建通过 APIRequestContext 发送的表单数据。

import com.microsoft.playwright.options.FormData;
// ...
FormData form = FormData.create()
.set("firstName", "John")
.set("lastName", "Doe")
.set("age", 30);
page.request().post("https://127.0.0.1/submit", RequestOptions.create().setForm(form));

方法

append

添加于: v1.44 formData.append

将新值追加到 FormData 对象内现有键的后面,或者添加键(如果尚不存在)。文件值可以作为 PathFilePayload 传递。可以添加多个同名字段。

FormData.set()FormData.append() 之间的区别在于,如果指定的键已存在,则 FormData.set() 将使用新值覆盖所有现有值,而 FormData.append() 将新值追加到现有值集的末尾。

import com.microsoft.playwright.options.FormData;
// ...
FormData form = FormData.create()
// Only name and value are set.
.append("firstName", "John")
// Name and value are set, filename and Content-Type are inferred from the file path.
.append("attachment", Paths.get("pic.jpg"))
// Name, value, filename and Content-Type are set.
.append("attachment", new FilePayload("table.csv", "text/csv", Files.readAllBytes(Paths.get("my-tble.csv"))));
page.request().post("https://127.0.0.1/submit", RequestOptions.create().setForm(form));

用法

FormData.append(name, value);

参数

返回


create

添加于: v1.18 formData.create

创建 FormData 的新实例。

用法

FormData.create();

返回


set

添加于: v1.18 formData.set

在表单上设置字段。文件值可以作为 PathFilePayload 传递。

import com.microsoft.playwright.options.FormData;
// ...
FormData form = FormData.create()
// Only name and value are set.
.set("firstName", "John")
// Name and value are set, filename and Content-Type are inferred from the file path.
.set("profilePicture1", Paths.get("john.jpg"))
// Name, value, filename and Content-Type are set.
.set("profilePicture2", new FilePayload("john.jpg", "image/jpeg", Files.readAllBytes(Paths.get("john.jpg"))))
.set("age", 30);
page.request().post("https://127.0.0.1/submit", RequestOptions.create().setForm(form));

用法

FormData.set(name, value);

参数

返回