跳到主要內容

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

參數

返回