FormData
Added in: v1.18The FormData is used create form data that is sent via APIRequestContext.
form = FormData()
form.set("firstName", "John")
form.set("lastName", "Doe")
form.set("age", 30)
await page.request.post("http://localhost/submit", form=form)form = FormData()
form.set("firstName", "John")
form.set("lastName", "Doe")
form.set("age", 30)
page.request.post("http://localhost/submit", form=form)Methods
form_data.append()
Added in: v1.44Appends 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 form_data.set() and form_data.append() is that if the specified key already exists, form_data.set() will overwrite all existing values with the new one, whereas form_data.append() will append the new value onto the end of the existing set of values.
form = FormData()
# Only name and value are set.
form.append("firstName", "John")
# Name and value are set, filename and Content-Type are inferred from the file path.
form.append("attachment", Path("pic.jpg"))
# Name, value, filename and Content-Type are set.
form.append("attachment", {
"name": "table.csv",
"mimeType": "text/csv",
"buffer": Path("my-table.csv").read_bytes(),
})
await page.request.post("http://localhost/submit", multipart=form)form = FormData()
# Only name and value are set.
form.append("firstName", "John")
# Name and value are set, filename and Content-Type are inferred from the file path.
form.append("attachment", Path("pic.jpg"))
# Name, value, filename and Content-Type are set.
form.append("attachment", {
"name": "table.csv",
"mimeType": "text/csv",
"buffer": Path("my-table.csv").read_bytes(),
})
page.request.post("http://localhost/submit", multipart=form)Arguments
Field name.
valuestring | boolean | int | path | ObjectField value.
namestringFile name
mime_typestringFile type
bufferBufferFile content
Returns
FormData
form_data.set()
Added in: v1.18Sets a field on the form. File values can be passed either as Path or as FilePayload.
form = FormData()
# Only name and value are set.
form.set("firstName", "John")
# Name and value are set, filename and Content-Type are inferred from the file path.
form.set("profilePicture1", Path("john.jpg"))
# Name, value, filename and Content-Type are set.
form.set("profilePicture2", {
"name": "john.jpg",
"mimeType": "image/jpeg",
"buffer": Path("john.jpg").read_bytes(),
})
form.set("age", 30)
await page.request.post("http://localhost/submit", multipart=form)form = FormData()
# Only name and value are set.
form.set("firstName", "John")
# Name and value are set, filename and Content-Type are inferred from the file path.
form.set("profilePicture1", Path("john.jpg"))
# Name, value, filename and Content-Type are set.
form.set("profilePicture2", {
"name": "john.jpg",
"mimeType": "image/jpeg",
"buffer": Path("john.jpg").read_bytes(),
})
form.set("age", 30)
page.request.post("http://localhost/submit", multipart=form)Arguments
Field name.
valuestring | boolean | int | path | ObjectField value.
namestringFile name
mime_typestringFile type
bufferBufferFile content
Returns
FormData