https://github.com/kodie/form-data-body
A tiny, dependency-free node module for generating a form's multipart/form-data body for a POST request.
https://github.com/kodie/form-data-body
body boundary data fields form formdata generate multipart node nodejs parse post request
Last synced: 7 months ago
JSON representation
A tiny, dependency-free node module for generating a form's multipart/form-data body for a POST request.
- Host: GitHub
- URL: https://github.com/kodie/form-data-body
- Owner: kodie
- License: mit
- Created: 2021-02-23T01:06:48.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2023-01-06T19:55:54.000Z (about 3 years ago)
- Last Synced: 2024-10-11T12:36:45.732Z (over 1 year ago)
- Topics: body, boundary, data, fields, form, formdata, generate, multipart, node, nodejs, parse, post, request
- Language: JavaScript
- Homepage:
- Size: 104 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
# form-data-body
[](https://www.npmjs.com/package/form-data-body)
[](https://travis-ci.com/kodie/form-data-body)
[](https://www.npmjs.com/package/form-data-body)
[](https://github.com/standard/standard)
[](license.md)
A tiny, dependency-free node module for generating a form's multipart/form-data body for a POST request.
This is meant to be a simple, no frills module for generating a string to send as the body for a POST request, simulating a form submission. If you need something with more bells and whistles, check out the [form-data](https://github.com/form-data/form-data) module.
## Installation
```shell
npm install --save form-data-body
```
## Usage
```js
const formDataBody = require('form-data-body')
// Specify form fields
const fields = {
name: 'My test post',
description: 'This is just a test post',
items: ['First Item', 'Second Item'],
// Files should be an object with the name, type, and data set to strings
image: {
name: 'hello.jpg',
type: 'image/jpeg',
data: binaryImageData
}
}
const boundary = formDataBody.generateBoundary()
const header = {
'Content-Type': `multipart/form-data; boundary=${boundary}`
}
const body = formDataBody(fields, boundary)
```
### Example Response
```
----------------------------071517909670537006900435
Content-Disposition: form-data; name="name"
My test post
----------------------------071517909670537006900435
Content-Disposition: form-data; name="description"
This is just a test post
----------------------------071517909670537006900435
Content-Disposition: form-data; name="items[]"
First Item
----------------------------071517909670537006900435
Content-Disposition: form-data; name="items[]"
Second Item
----------------------------071517909670537006900435
Content-Disposition: form-data; name="image"; filename="hello.jpg"
Content-Type: image/jpeg
[BINARY IMAGE DATA]
----------------------------071517909670537006900435--
```
## License
MIT. See the [license.md file](license.md) for more info.