Skip to main content

FormData

FormData 用于创建通过 APIRequestContext 发送的表单数据。

🌐 The 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)
page.request.post("http://localhost/submit", form=form)

方法

🌐 Methods

append

Added in: v1.44 formData.append

将一个新值附加到 FormData 对象中已存在的键,或者如果该键不存在则添加该键。文件值可以以 PathFilePayload 的形式传递。可以添加多个同名字段。

🌐 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.

form_data.set()form_data.append() 的区别在于,如果指定的键已经存在,form_data.set() 会用新值覆盖所有已有的值,而 form_data.append() 会将新值添加到现有值集合的末尾。

🌐 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(),
})
page.request.post("http://localhost/submit", multipart=form)

用法

form_data.append(name, value)

参数

返回


set

Added in: v1.18 formData.set

在表单上设置一个字段。文件值可以以 PathFilePayload 的形式传递。

🌐 Sets 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)
page.request.post("http://localhost/submit", multipart=form)

用法

form_data.set(name, value)

参数

返回