How to use FormData in node.js without Browser?

Adelin Ionut picture Adelin Ionut · Aug 25, 2020 · Viewed 8.2k times · Source

I want to make a post request in nodejs without browser since it is backend code.

const formdata = new FormData()
formdata.append('chartfile', file);

But above code gives me error as FormData not defined. I am working with ES6.

Anybody, who can let me know how to use the FormData in nodejs?

Answer

Radical Edward picture Radical Edward · Aug 25, 2020

You should use form-data - npm module. because formData() isn't NodeJS API

Use it this way,

var FormData = require('form-data');
var fs = require('fs');
 
var form = new FormData();
form.append('my_field', 'my value');
form.append('my_buffer', new Buffer(10));
form.append('my_file', fs.createReadStream('/foo/bar.jpg'));