A library to create readable multipart/form-data streams for form and file uploads.
npx @tessl/cli install tessl/npm-form-data@4.0.0Form-Data is a Node.js library that creates readable multipart/form-data streams for submitting forms and uploading files to web applications. It provides a streaming interface inspired by XMLHttpRequest-2 FormData with extensive customization options for headers, boundaries, and content types.
npm install form-dataconst FormData = require('form-data');For TypeScript:
import FormData = require('form-data');
// or
import * as FormData from 'form-data';Note: This package uses CommonJS exports (export = FormData), not ES modules.
const FormData = require('form-data');
const fs = require('fs');
// Create form instance
const form = new FormData();
// Append various data types
form.append('my_field', 'my value');
form.append('my_buffer', Buffer.from('hello'));
form.append('my_file', fs.createReadStream('/path/file.jpg'));
// Submit directly
form.submit('http://example.org/upload', (err, res) => {
if (err) throw err;
console.log('Upload completed!');
res.resume();
});Form-Data is built around several key components:
Core functionality for building multipart forms by appending fields, files, and streams with customizable options.
import * as stream from 'stream';
class FormData extends stream.Readable {
constructor(options?: Options);
append(field: string, value: any, options?: AppendOptions | string): void;
// Static properties
static LINE_BREAK: string;
static DEFAULT_CONTENT_TYPE: string;
}
interface Options {
writable?: boolean;
readable?: boolean;
dataSize?: number;
maxDataSize?: number;
pauseStreams?: boolean;
// Additional options from ReadableOptions
highWaterMark?: number;
encoding?: string;
objectMode?: boolean;
autoDestroy?: boolean;
}
interface AppendOptions {
header?: string | Headers;
knownLength?: number;
filename?: string;
filepath?: string;
contentType?: string;
}Direct HTTP submission capabilities with automatic header management and content-length calculation.
submit(
params: string | SubmitOptions,
callback?: (error: Error | null, response: http.IncomingMessage) => void
): http.ClientRequest;
interface SubmitOptions extends http.RequestOptions {
protocol?: 'https:' | 'http:';
}Header generation and boundary management for multipart streams, including custom boundary setting and header merging.
getHeaders(userHeaders?: Headers): Headers;
setBoundary(boundary: string): void;
getBoundary(): string;
interface Headers {
[key: string]: any;
}Content length calculation for streams and form data, both synchronous and asynchronous methods.
getLength(callback: (err: Error | null, length: number) => void): void;
getLengthSync(): number;
hasKnownLength(): boolean;Buffer generation for form data and stream integration with other HTTP clients.
getBuffer(): Buffer;
toString(): string;Browser compatibility layer that provides fallback to native FormData in browser environments.
// In browser environments, exports native FormData
module.exports = typeof self === 'object' ? self.FormData : window.FormData;import * as stream from 'stream';
import * as http from 'http';
type Headers = { [key: string]: any };
interface ReadableOptions {
highWaterMark?: number;
encoding?: string;
objectMode?: boolean;
read?(this: stream.Readable, size: number): void;
destroy?(this: stream.Readable, error: Error | null, callback: (error: Error | null) => void): void;
autoDestroy?: boolean;
}
interface Options extends ReadableOptions {
writable?: boolean;
readable?: boolean;
dataSize?: number;
maxDataSize?: number;
pauseStreams?: boolean;
}
interface AppendOptions {
header?: string | Headers;
knownLength?: number;
filename?: string;
filepath?: string;
contentType?: string;
}
interface SubmitOptions extends http.RequestOptions {
protocol?: 'https:' | 'http:';
}