FormData
The FormData class provides a way to construct key/value pairs representing form fields and their values.
It is mainly used for building multipart/form-data requests that can include both text and binary data (such as files or images).
In the Scripting app, FormData is fully compatible with the standard Fetch API, allowing you to send data through fetch() with mixed text and file fields.
Definition
Purpose
FormData is used to:
- Build
multipart/form-datarequests with text and file fields. - Upload files (images, documents, audio, etc.) with metadata.
- Replace JSON-based bodies when binary data is included.
Methods
append(name: string, value: string): void
append(name: string, value: Data, mimeType: string, filename?: string): void
Appends a new field to the form. Can be used to add both text and file fields.
Parameters
Example
set(name: string, value: string): void
set(name: string, value: Data, filename?: string): void
Sets a form field, replacing any existing field with the same name.
Unlike append(), this overwrites previous values.
Example
get(name: string): string | Data | null
Retrieves the value of a form field.
Returns null if the field does not exist.
Example
getAll(name: string): any[]
Returns all values associated with a given field name (useful when using multiple append() calls with the same name).
Example
has(name: string): boolean
Checks whether a form field exists.
Example
delete(name: string): void
Deletes the specified field and all of its values.
Example
forEach(callback: (value: any, name: string, parent: FormData) => void): void
Iterates over all form fields and invokes the callback function for each one.
Example
entries(): [string, any][]
Returns an array of [name, value] pairs for all form fields.
Example
toJson(): Record<string, any>
Converts the form data into a plain JSON object for debugging or logging.
If the form contains binary data (Data objects), those fields will be represented as descriptive placeholders instead of raw binary data.
Example
Usage Examples
Example 1 — Upload a File
Example 2 — Upload Multiple Files
Example 3 — Mixed Text and File Fields
Example 4 — Iterating and Debugging
Relationships with Other Classes
Notes
- Automatic Content-Type: When you pass a
FormDatainstance tofetch(), the app automatically sets the correctContent-Typeheader (with a boundary). Do not manually set it. - Multiple Fields: Use
append()to add multiple values with the same name. - Binary Uploads: Always provide a MIME type for binary uploads. Otherwise, the data defaults to
application/octet-stream. - JSON Conversion: The
toJson()method is intended for debugging and should not be used for actual data transmission.
Summary
FormData is the core class for building multipart/form-data requests in the Scripting app.
It provides a flexible and developer-friendly API for combining text and binary fields, integrating seamlessly with fetch() and Data.
Key Features
- Fully compatible with the Fetch API
- Supports text and file uploads
- Automatically handles multipart boundaries
- Useful debugging utilities (
entries(),toJson()) - Integrates natively with
Data,Request, andResponse
