npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

request-multipart

v1.0.0

Published

Multipart body support for request-compose

Downloads

433,389

Readme

request-multipart

npm-version travis-ci coveralls-status

Multipart body support for request-compose

var request = require('request-compose').extend({
  Request: {multipart: require('request-multipart')}
}).client

;(async () => {
  try {
    var {res, body} = await request({
      method: 'POST',
      url: 'https://slack.com/api/files.upload',
      headers: {
        authorization: 'Bearer [ACCESS TOKEN]'
      },
      multipart: {
        file: require('fs').createReadStream('cat.png')
      }
    })
    console.log(body)
  }
  catch (err) {
    console.error(err)
  }
})()

multipart/form-data

multipart: {
  key: 'value' // String, Buffer or Stream
}
multipart: {
  status: 'Hello',
  'media[]': fs.createReadStream('cat.png')
}
--88b7fcd3-7776-4087-8b09-5b0f5c8af069
Content-Disposition: form-data; name="status"
Content-Type: text/plain

Hello

--88b7fcd3-7776-4087-8b09-5b0f5c8af069
Content-Disposition: form-data; name="media[]"; filename="cat.png"
Content-Type: image/png

...stream...

Options

Most servers expect the filename key to be set inside the Content-Disposition and a correct MIME type to be specified as Content-Type. When reading a file as Buffer request-multipart cannot determine those values.

multipart: {
  key: {
    body: 'value', // String, Buffer or Stream
    options: {name: '', type: '', length: 0}
  }
}
multipart: {
  file: {
    body: fs.readFileSync('cat.png'),
    options: {name: 'cat.png', type: 'image/png'}
  }
}
--77f9de0f-8905-4b63-9ca9-ad1ff9827053
Content-Disposition: form-data; name="file"; filename="cat.png"
Content-Type: image/png

...buffer...

List

multipart: {
  key: [
    'value', // String, Buffer or Stream
    'value', // String, Buffer or Stream
  ]
}
multipart: {
  attachment: [
    fs.createReadStream('cat.png'),
    fs.createReadStream('beep.mp3')
  ]
}
--21cd47c7-379b-43f3-8e5e-d95c36653abf
Content-Disposition: form-data; name="attachment"; filename="cat.png"
Content-Type: image/png

...stream...

--21cd47c7-379b-43f3-8e5e-d95c36653abf
Content-Disposition: form-data; name="attachment"; filename="beep.mp3"
Content-Type: audio/mpeg

...stream...

multipart/related

multipart: [
  {
    key: 'value',
    body: 'value' // String, Buffer or Stream
  },
  {
    key: 'value',
    body: 'value' // String, Buffer or Stream
  }
]
multipart: [
  {
    'Content-Type': 'application/json',
    body: JSON.stringify({name: 'cat.png'})
  },
  {
    'Content-Type': 'image/png',
    body: fs.createReadStream('cat.png')
  }
]
--f3386c29-f73f-423f-b80e-9dac2801893b
Content-Type: application/json

{"name":"cat.png"}

--f3386c29-f73f-423f-b80e-9dac2801893b
Content-Type: image/png

...stream...

examples

DEBUG=req,res,body,json node examples/multipart.js slack