Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
object-to-formdata
Advanced tools
> Convenient JavaScript function that serializes Objects to FormData instances.
The object-to-formdata npm package is a utility that converts JavaScript objects into FormData instances. This is particularly useful for sending data via HTTP requests, especially when dealing with file uploads or complex nested objects.
Basic Object to FormData Conversion
This feature allows you to convert a simple JavaScript object into a FormData instance. The code sample demonstrates converting an object with basic key-value pairs into FormData.
const objectToFormData = require('object-to-formdata');
const obj = { name: 'John', age: 30 };
const formData = objectToFormData(obj);
console.log([...formData.entries()]);
Nested Object Conversion
This feature supports converting nested objects into FormData. The code sample shows how a nested object structure is handled and converted.
const objectToFormData = require('object-to-formdata');
const obj = { user: { name: 'John', age: 30 }, active: true };
const formData = objectToFormData(obj);
console.log([...formData.entries()]);
Array Handling
This feature allows arrays within objects to be converted into FormData. The code sample demonstrates converting an object containing an array into FormData.
const objectToFormData = require('object-to-formdata');
const obj = { tags: ['node', 'javascript', 'npm'] };
const formData = objectToFormData(obj);
console.log([...formData.entries()]);
File Uploads
This feature supports file uploads by converting file streams into FormData. The code sample shows how to include a file in the object and convert it into FormData.
const objectToFormData = require('object-to-formdata');
const fs = require('fs');
const obj = { file: fs.createReadStream('path/to/file.txt') };
const formData = objectToFormData(obj);
console.log([...formData.entries()]);
The form-data package is a module to create readable 'multipart/form-data' streams. It is more low-level compared to object-to-formdata and requires manual handling of data structures, but it offers more control over the form data creation process.
Axios is a popular HTTP client that supports sending FormData directly. While it is not specifically designed for converting objects to FormData, it can handle FormData instances and is often used in conjunction with other utilities like object-to-formdata.
The formdata-polyfill package is a polyfill for the FormData API, providing a consistent implementation across different environments. It does not convert objects to FormData directly but ensures that FormData is available and works as expected in environments that do not support it natively.
Convenient JavaScript function that serializes Objects to FormData instances.
npm install object-to-formdata
NOTE: STARTING WITH VERSION 4.0.0, THE NAMED EXPORT HAS CHANGED!
NOTE: STARTING WITH VERSION 3.0.0, THERE IS NO DEFAULT EXPORT!
import { serialize } from 'object-to-formdata';
const object = {
/**
* key-value mapping
* values can be primitives or objects
*/
};
const options = {
/**
* include array indices in FormData keys
* defaults to false
*/
indices: false,
/**
* treat null values like undefined values and ignore them
* defaults to false
*/
nullsAsUndefineds: false,
/**
* convert true or false to 1 or 0 respectively
* defaults to false
*/
booleansAsIntegers: false,
};
const formData = serialize(
object,
options, // optional
existingFormData, // optional
keyPrefix, // optional
);
console.log(formData);
FAQs
> Convenient JavaScript function that serializes Objects to FormData instances.
The npm package object-to-formdata receives a total of 111,622 weekly downloads. As such, object-to-formdata popularity was classified as popular.
We found that object-to-formdata demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.