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.