Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

request-multipart

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

request-multipart

Multipart body support for request-compose

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
104K
increased by2.61%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 24 Jan 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc