Dead simple way of producing multipart/*
messages. Works in browser, node, or anywhere else (hopefully) thanks to its simplicity.
Its suitable for small messages (say, up to few kB), larger would benefit greatly from stream based approach.
npm install multipartish --save
Hello world
var MultiPartish = require('multipartish')
console.log(new MultiPartish().header("Content-Type", "text/plain").part("hello world").get())
This example shows how to upload file to Google drive
// prepare data
var MultiPartish = require('multipartish')
var name = "My File"
var body = "Hello world!\n"
var m = new MultiPartish()
m.header("Content-Type", "application/json; charset=UTF-8")
m.part(JSON.stringify(name: name))
m.header("Content-Type", "text/plain")
m.part(body)
var body_data = m.get()
var contentType = m.contentType()
// upload
gapi.client.request({
'path': 'https://www.googleapis.com/upload/drive/v3/files',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': { 'Content-Type': contentType },
'body': body_data
})
Constructor accepts a single object as the parameter, encapsulating the optional arguments. Only one key is currently defined:
boundary
: sets the boundary
Examples:
var MultiPartish = require('multipartish')
var a = new MultiPartish()
var b = new MultiPartish({boundary: 'my-boundary'})
Returns the complete multipart message.
Adds a part and returns this
.
Specifies a header for following part, for example .header("Content-Type: text/plain")
. Returns this
.
Specifies a header for following part, for example .header('Content-Type', 'text/plain')
. Returns this
.
Equavalent to:
header(name + ": " + value)
Returns multipart/mixed; boundary=XXX
.
Returns multipart/something; boundary=XXX
. According to RFC
you can set something
to: mixed
, alternative
, digest
or parallel
.
Boundary of multipart message. Consider this property read-only, if you feel you want to set this property, you should use the contructor argument instead.
0.1.0 - initial release
0.1.1 - fluent api support
0.1.2 - README.md fixes, not yet published to npm