js-to-formdata
v1.2.6
Published
Function that converts the javascript object to an instance of Form-data
Downloads
404
Maintainers
Readme
js-to-formdata
Module that makes it possible and easy for you to convert nested javascript objects directly to Form-Data.
Supports objects with property values of the primitives javascript types, as well as arrays, buffers and file streams.
Install
npm i js-to-formdata
Usage
Object containing nested objects example:
const convertObjectToFormData = require('js-to-formdata');
const originalObject = {
property: {
nestedProperty: 'value',
},
anotherProperty: 'value',
};
const formData = convertObjectToFormData(originalObject);
The example above produces form data equivalent to the result of executing the following commands using the form-data lib:
form.append('propertyNestedProperty', 'value');
form.append('anotherProperty', 'value');
Object containing array example:
const convertObjectToFormData = require('js-to-formdata');
const originalObjectWithArray = {
items: [{ property: 'value' }],
anotherProperty: 'value',
};
const formData = convertObjectToFormData(originalObject);
The example above produces form data equivalent to the result of executing the following commands using the form-data lib:
form.append('itemProperty1', 'value');
form.append('anotherProperty', 'value');
Functions will be ignored:
const convertObjectToFormData = require('js-to-formdata');
const originalObject = {
propertyFunction: () => {},
anotherProperty: 'value',
};
const formData = convertObjectToFormData(originalObject);
The example above produces form data equivalent to the result of executing the following commands using the form-data lib:
form.append('anotherProperty', 'value');
Integration with other libraries
Use as exemplified by Form-Data documentation.
Request
Form submission using request:
const convertObjectToFormData = require('js-to-formdata');
const originalObject = {
property: 'value',
};
const formData = convertObjectToFormData(originalObject);
request.post({ url:'http://service.com/upload', formData }, function(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
node-fetch
You can submit a form using node-fetch:
const convertObjectToFormData = require('js-to-formdata');
const originalObject = {
property: 'value',
};
const formData = convertObjectToFormData(originalObject);
fetch('http://example.com', { method: 'POST', body: formData })
.then(function(res) {
return res.json();
}).then(function(json) {
console.log(json);
});
axios
You can also submit a form using axios:
const convertObjectToFormData = require('js-to-formdata');
const originalObject = {
property: 'value',
};
const formData = convertObjectToFormData(originalObject);
axios.post('http://example.com', formData, {
headers: {
...formHeaders,
},
})
.then(response => response)
.catch(error => error)
Custom options
You can use custom options Form-Data, passing to function module an instance of FormData with the already defined options.
Example:
const FormData = require('form-data');
const convertObjectToFormData = require('js-to-formdata');
const form = new FormData({ maxDataSize: 20971520 });
const originalObject = {
property: 'value',
};
const formData = convertObjectToFormData(originalObject, { form });