English · Java
Appearance
English · Java
Appearance
The FormData is used create form data that is sent via 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));Appends a new value onto an existing key inside a FormData object, or adds the key if it does not already exist. File values can be passed either as Path or as FilePayload. Multiple fields with the same name can be added.
The difference between formData.set() and formData.append() is that if the specified key already exists, formData.set() will overwrite all existing values with the new one, whereas formData.append() will append the new value onto the end of the existing set of values.
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));Arguments
Field name.
value string | boolean | int | path | Object
Field value.
name string
File name
mimeType string
File type
buffer Buffer
File content
Returns
FormDataCreates new instance of FormData.
Returns
FormDataSets a field on the form. File values can be passed either as Path or as 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));Arguments
Field name.
value string | boolean | int | path | Object
Field value.
name string
File name
mimeType string
File type
buffer Buffer
File content
Returns
FormData