Implementation:Microsoft Playwright FormData
Overview
MultipartFormData builds multipart/form-data request bodies for HTTP requests, supporting both text fields and file uploads with proper MIME type detection.
Description
The MultipartFormData class constructs RFC 2046 compliant multipart form data payloads. It generates a unique boundary string and provides methods to add text fields and file fields. File fields automatically detect MIME types using the mime library based on file extension. The class produces a properly formatted Buffer suitable for use as an HTTP request body.
Usage
Used internally when Playwright needs to submit form data with file uploads through the API request context.
Code Reference
Source Location
packages/playwright-core/src/server/formData.ts (92 lines)
Class Signature
export class MultipartFormData {
constructor()
contentTypeHeader(): string
addField(name: string, value: string): void
addFileField(name: string, value: { name: string; mimeType?: string; buffer: Buffer }): void
finish(): Buffer
}
Import
import { MultipartFormData } from './server/formData';
I/O Contract
Inputs
- Text fields: name-value string pairs
- File fields: name, file name, optional MIME type, and file buffer
Outputs
contentTypeHeader()returns the Content-Type header including the boundaryfinish()returns the complete multipart body as aBuffer
Usage Examples
Building Form Data
const form = new MultipartFormData();
form.addField('username', 'testuser');
form.addFileField('avatar', { name: 'photo.png', buffer: fileBuffer });
const body = form.finish();
const contentType = form.contentTypeHeader();
Related Pages
- Microsoft_Playwright_NetworkUtils -- Network request utilities