Skip to content

Simple builder to create form-data to be send via nodejs' http/https module without using any npm packages.

License

Notifications You must be signed in to change notification settings

borisskert/simple-multipart-form-data.js

Repository files navigation

simple multipart form data

Simple builder to create form-data to be send via nodejs' http/https module without using any npm packages.

Build

No build necessary! Just copy the file multipart-form-data.js into your project!

Usage

Build and send multipart form-data

Append key/value pairs:

import {MultipartFormDataBuilder} from './multipart-form-data'

const formDataBuilder = MultipartFormDataBuilder()

formDataBuilder.append('first_name', 'Boris')
formDataBuilder.append('last_name', 'Skert')
formDataBuilder.append('year_of_birth', 1980)

Append a file:

formDataBuilder.appendFile(
  'my_upload_file',
  Buffer.from('abc'),
  'picture.jpg',
  'image/jpeg'
)

Send via nodejs' http module:

const http = require('http')

const formData = formDataBuilder.build()

const options = {
  hostname: 'localhost',
  path: '/api/data',
  method: 'POST',
  headers: formData.headers
}

const query = http.request(options, (response) => {
  response.on('data', chunk => {
    //...
  })
  response.on('end', () => {
    //...
  })
})

query.on('error', (e) => {
  console.log(e)
})

query.write(formData.body);
query.end();

Parse multipart form-data request

 const headers = ...
 const body = ...
 const parsedBody = MultipartFormData(headers, body).parse()

Run tests

$ npm install
$ npm run test

About

Simple builder to create form-data to be send via nodejs' http/https module without using any npm packages.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published