Categories
JavaScript Answers

How to use FormData in Node.js without browser?

Spread the love

To use FormData in Node.js without browser, we use the URLSearchParams constructor.

For instance, we write

const fs = require("fs");

const form = new URLSearchParams();
form.append("my_field", "my value");
form.append("my_buffer", new Buffer(10));
form.append("my_file", fs.createReadStream("/foo/bar.jpg"));

to create a URLSearchParams object.

Then we call append with the key and value of each form data entry to add it.

We can add buffers and read streams for binary data.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *