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("http://localhost/submit", RequestOptions.create().setForm(form));
方法
append
新增于: v1.44将一个新值附加到 FormData 对象内现有键上,如果该键不存在则添加该键。文件值可以作为 Path
或作为 FilePayload
传递。可以添加具有相同名称的多个字段。
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("http://localhost/submit", RequestOptions.create().setForm(form));
用法
FormData.append(name, value);
参数
返回值
create
新增于: v1.18创建 FormData 的新实例。
用法
FormData.create();
返回值
set
新增于: v1.18在表单上设置一个字段。文件值可以作为 Path
或作为 FilePayload
传递。
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("http://localhost/submit", RequestOptions.create().setForm(form));
用法
FormData.set(name, value);
参数
返回值