Build multipart stream, with stream.
Unlike form-data, this library provides more lower-level APIs to build not only multipart/form-data
but also multipart/related
and any other similar structures.
Node >= 4, tested on latest Node and latest LTS Node.
npm install --save multipartist
import Multipart from 'multipartist';
import request from 'request';
import { createReadStream } from 'fs';
const multipart = new Multipart('form-data');
multipart.append(JSON.stringify({ 'text': 'Hello world!' }), {
'Content-Disposition': 'form-data; name="metadata"',
'Content-Type': 'application/json; charset=UTF-8',
});
multipart.append(createReadStream('audio.wav'), {
'Content-Disposition': 'form-data; name="audio"',
'Content-Type': 'application/octet-stream',
'X-Speaker-Name': 'XiNGRZ',
});
multipart.pipe(request.post('https://api.example.com/recognize', {
headers: multipart.headers({
'Authorization': 'Bearer YOUR_OWN_API_KEY_HERE',
}),
}, (req, res, body) => {
// ...
}));
The Multipartist class, which is an subclass of Readable Stream.
- type String - Multipart type (e.g.
form-data
,related
, etc...). Defaults toform-data
. - options Object
- endOnEmpty=true - Whether the Multipartist readable should end when it's empty. Otherwise, it won't end until explicitly call
flush()
- endOnEmpty=true - Whether the Multipartist readable should end when it's empty. Otherwise, it won't end until explicitly call
Add a part.
- content String | Buffer | Readable - Content of this part
- length Number - Optional. Length of the content of this part. It's better to specific the length of a
Readable
part explicitly. Otherwise, theContent-Length
of the whole multipart payload would not be caculated. - headers Object - Optional. Additional headers of this part
Multipartist - the Multipartist instance itself
Returns all auto-generated headers.
- headers Object - Optional. Additional headers
- Object - Headers
Flush the ending data and end the stream. Only required if endOnEmpty
is explicitly set to false
.
npm test
This project is released under the terms of MIT License.