A library to create readable multipart/form-data streams for form and file uploads.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Buffer generation for form data and stream integration, providing synchronous access to complete multipart form content for use with HTTP clients and custom implementations.
Returns the complete form data as a Buffer for synchronous access to multipart content.
/**
* Return complete form data as Buffer (synchronous)
* @returns Buffer containing entire multipart form-data
* @note Only works with non-stream content (strings, Buffers, etc.)
*/
getBuffer(): Buffer;Usage Examples:
const FormData = require('form-data');
// Form with buffer-compatible content
const form = new FormData();
form.append('name', 'Alice');
form.append('age', '25');
form.append('data', Buffer.from('binary data'));
form.append('json', JSON.stringify({ key: 'value' }));
// Get complete form as buffer
const buffer = form.getBuffer();
console.log('Form size:', buffer.length, 'bytes');
console.log('Content type header needed:', form.getHeaders()['content-type']);
// Use with Axios
const axios = require('axios');
axios.post('http://example.com/upload', buffer, {
headers: form.getHeaders(),
maxContentLength: Infinity
}).then(response => {
console.log('Upload successful');
});
// Use with other HTTP clients
const http = require('http');
const request = http.request({
method: 'POST',
host: 'example.com',
path: '/api',
headers: {
...form.getHeaders(),
'Content-Length': buffer.length
}
});
request.write(buffer);
request.end();The getBuffer() method only works with content that can be converted to Buffers:
const FormData = require('form-data');
const fs = require('fs');
// Supported types for getBuffer()
const form = new FormData();
form.append('string', 'text content');
form.append('number', 42); // converted to string
form.append('buffer', Buffer.from('data'));
form.append('boolean', true); // converted to string
const buffer = form.getBuffer(); // Works fine
// Unsupported types - will cause errors
const form2 = new FormData();
form2.append('file', fs.createReadStream('file.txt')); // ReadStream
// form2.getBuffer(); // This would fail!
// For streams, use pipe() or submit() instead
form2.pipe(process.stdout); // Stream to output
form2.submit('http://example.com/upload', callback); // HTTP submissionProvides string representation of the FormData object.
/**
* String representation of FormData instance
* @returns '[object FormData]'
* @note Don't use for actual form data - use getBuffer() instead
*/
toString(): string;Usage Examples:
const FormData = require('form-data');
const form = new FormData();
console.log(form.toString()); // '[object FormData]'
console.log(String(form)); // '[object FormData]'
// For debugging and type checking
console.log(Object.prototype.toString.call(form)); // '[object FormData]'
// Don't use toString() for form content
// Wrong:
// const content = form.toString(); // Just returns '[object FormData]'
// Correct:
const content = form.getBuffer(); // Actual form dataAxios Integration:
const axios = require('axios');
const FormData = require('form-data');
const form = new FormData();
form.append('field1', 'value1');
form.append('field2', Buffer.from('binary'));
// Method 1: Using getBuffer()
const buffer = form.getBuffer();
const response = await axios.post('http://api.example.com/upload', buffer, {
headers: form.getHeaders()
});
// Method 2: Using stream (for files)
const form2 = new FormData();
form2.append('file', fs.createReadStream('document.pdf'));
const response2 = await axios.post('http://api.example.com/upload', form2, {
headers: form2.getHeaders()
});Node-fetch Integration:
const fetch = require('node-fetch');
const FormData = require('form-data');
const form = new FormData();
form.append('data', 'content');
// Using as stream (recommended)
const response = await fetch('http://example.com/api', {
method: 'POST',
body: form, // form-data implements stream interface
headers: form.getHeaders()
});
// Using buffer (for small forms)
const buffer = form.getBuffer();
const response2 = await fetch('http://example.com/api', {
method: 'POST',
body: buffer,
headers: form.getHeaders()
});Request Library Integration:
const request = require('request');
const FormData = require('form-data');
const form = new FormData();
form.append('field', 'value');
// Method 1: Using form directly
request.post({
url: 'http://example.com/upload',
body: form,
headers: form.getHeaders()
}, callback);
// Method 2: Using buffer
const buffer = form.getBuffer();
request.post({
url: 'http://example.com/upload',
body: buffer,
headers: form.getHeaders()
}, callback);const FormData = require('form-data');
const net = require('net');
const form = new FormData();
form.append('message', 'Hello Server');
const buffer = form.getBuffer();
const headers = form.getHeaders();
// Raw TCP socket example
const socket = net.createConnection(80, 'example.com');
socket.write('POST /upload HTTP/1.1\r\n');
socket.write('Host: example.com\r\n');
socket.write(`Content-Type: ${headers['content-type']}\r\n`);
socket.write(`Content-Length: ${buffer.length}\r\n`);
socket.write('\r\n');
socket.write(buffer);
socket.on('data', (data) => {
console.log('Response:', data.toString());
});const FormData = require('form-data');
const form = new FormData();
form.append('field1', 'value1');
form.append('field2', 'value2');
const buffer = form.getBuffer();
// Inspect raw multipart content
console.log('Raw form data:');
console.log(buffer.toString());
// Output will show:
// --------------------------boundary123456789
// Content-Disposition: form-data; name="field1"
//
// value1
// --------------------------boundary123456789
// Content-Disposition: form-data; name="field2"
//
// value2
// --------------------------boundary123456789--
// Parse boundary from content-type
const contentType = form.getHeaders()['content-type'];
const boundary = contentType.split('boundary=')[1];
console.log('Boundary:', boundary);// For large forms, consider memory usage:
const FormData = require('form-data');
// Small form - getBuffer() is fine
const smallForm = new FormData();
smallForm.append('data', 'small content');
const buffer = smallForm.getBuffer(); // ~few KB
// Large form - use streaming
const largeForm = new FormData();
largeForm.append('file', fs.createReadStream('largefile.zip'));
// Don't use getBuffer() - would load entire file into memory
// Use pipe() or submit() instead
largeForm.pipe(writeStream);