
Security News
ESLint Adds Official Support for Linting HTML
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
Simple multipart/form-data implementation with automatic data type detection. Supports: Strings, Numbers, Arrays, Streams, Buffers and Vinyl.
A multi-part
allows you to create multipart/form-data Stream
and Buffer
, which can be used to submit forms and file uploads to other web applications.
It extends multi-part-lite
and adds automatic data type detection.
Supports: Strings
, Numbers
, Arrays
, ReadableStreams
, Buffers
and Vinyl
.
$ npm install multi-part --save
Usage with got
as Stream
:
const got = require('got');
const Multipart = require('multi-part');
const form = new Multipart();
form.append('photo', got.stream('https://avatars1.githubusercontent.com/u/2401029'));
form.append('field', 'multi-part test');
(async () => {
const body = await form.stream();
got.post('127.0.0.1:3000', { headers: form.getHeaders(), body });
})()
Usage with got
as Buffer
:
const got = require('got');
const Multipart = require('multi-part');
const form = new Multipart();
form.append('photo', got.stream('https://avatars1.githubusercontent.com/u/2401029'));
form.append('field', 'multi-part test');
(async () => {
const body = await form.buffer();
got.post('127.0.0.1:3000', { headers: form.getHeaders(false), body });
})()
Usage with http
/https
as Stream
:
const http = require('http');
const https = require('https');
const Multipart = require('multi-part');
const form = new Multipart();
form.append('photo', https.request('https://avatars1.githubusercontent.com/u/2401029'));
(async () => {
const stream = await form.stream();
stream.pipe(http.request({ headers: form.getHeaders(), hostname: '127.0.0.1', port: 3000, method: 'POST' }));
})()
Usage with http
/https
as Buffer
:
const http = require('http');
const https = require('https');
const Multipart = require('multi-part');
const form = new Multipart();
form.append('photo', https.request('https://avatars1.githubusercontent.com/u/2401029'));
(async () => {
const body = await form.buffer();
const req = http.request({ headers: form.getHeaders(false), hostname: '127.0.0.1', port: 3000, method: 'POST' });
req.end(body);
})()
Constructor.
Object
with options:
multipart
data. Ex: if equal CustomBoundary
, boundary will be equal exactly CustomBoundary
.multipart
data. Ex: if equal CustomBoundary
, boundary will be equal something like --CustomBoundary567689371204
.Object
with defaults values:
filename
is not specified in the options of .append
method. By default file
.filename
is not specified in the options of .append
method. By default bin
.contentType
is not specified in the options of .append
method. By default application/octet-stream
.const Multipart = require('multi-part');
const { MultipartAsync } = require('multi-part');
Adds a new data to the multipart/form-data
stream.
photo
.String
, Number
, Array
, Buffer
, ReadableStream
or even Vynil.anonim.jpg
.filename
and contentType
empty and it will be automatically determined, if possible. Ex: image/jpeg
.If value
is an array, append
will be called for each value:
form.append('array', [0, [2, 3], 1]);
// similar to
form.append('array', 0);
form.append('array', 2);
form.append('array', 3);
form.append('array', 1);
Null
, false
and true
will be converted to '0'
, '0'
and '1'
. Numbers will be converted to strings also.
For Buffer
and ReadableStream
content type will be automatically determined, if it's possible, and name will be specified according to content type. If content type is image/jpeg
, file name will be set as file.jpeg
(if filename
option is not specified).
In case content type is undetermined, content type and file name will be set as application/octet-stream
and file.bin
.
Returns a Promise
with a multipart/form-data
stream.
Returns a Promise
with a buffer of the multipart/form-data
stream data.
Returns the form boundary used in the multipart/form-data
stream.
form.getBoundary(); // -> '--MultipartBoundary352840693617'
Returns the length of a buffer of the multipart/form-data
stream data.
Should be called after .buffer()
;
For .stream()
it's always 0
.
await form.buffer();
form.getLength(); // -> 12345
Returns the headers.
If you want to get correct content-length
, you should call it after .buffer()
. There is no way to know content-length
of the .stream()
, so it will be always 0
.
false
- headers will include content-length
header, otherwise there will be transfer-encoding: 'chunked'
.form.getHeaders(); // ->
//{
// 'transfer-encoding': 'chunked',
// 'content-type': 'multipart/form-data; boundary="--MultipartBoundary352840693617"'
//}
With .buffer()
:
form.getHeaders(false); // ->
//{
// 'content-length': '0',
// 'content-type': 'multipart/form-data; boundary="--MultipartBoundary352840693617"'
//}
await form.buffer();
form.getHeaders(false); // ->
//{
// 'content-length': '12345',
// 'content-type': 'multipart/form-data; boundary="--MultipartBoundary352840693617"'
//}
The MIT License (MIT)
Copyright (c) 2015-2022 Alexey Bystrov
FAQs
Simple multipart/form-data implementation with automatic data type detection. Supports: Strings, Numbers, Arrays, Streams, Buffers and Vinyl.
The npm package multi-part receives a total of 10,630 weekly downloads. As such, multi-part popularity was classified as popular.
We found that multi-part demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.