跳至主要内容

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

参数

返回值