Uploading a file using a POST request in Node.js

I have a problem downloading a file using a POST request in Node.js. I have to use the request module to accomplish this (without external npms). The server requires it to be a multidisciplinary request with the file field containing the file data. This seems to be easy to do in Node.js without using an external module.

I tried using this example , but to no avail:

 request.post({ uri: url, method: 'POST', multipart: [{ body: '<FILE_DATA>' }] }, function (err, resp, body) { if (err) { console.log('Error!'); } else { console.log('URL: ' + body); } }); 
+43
multipartform-data
Aug 16 '14 at 23:15
source share
4 answers

It looks like you are already using request module .

in this case, all you need to send multipart/form-data is to use the form function:

 var req = request.post(url, function (err, resp, body) { if (err) { console.log('Error!'); } else { console.log('URL: ' + body); } }); var form = req.form(); form.append('file', '<FILE_DATA>', { filename: 'myfile.txt', contentType: 'text/plain' }); 

but if you want to publish any existing file from your file system, you can simply pass it as a readable stream:

 form.append('file', fs.createReadStream(filepath)); 

request will retrieve all associated metadata on its own.

For more information on publishing multipart/form-data see the node-form-data module , which is internally used by request .

+63
Aug 17 '14 at 0:00
source share

The undocumented function of the formData field implemented by request is the ability to pass parameters to the form-data module, which uses:

 request({ url: 'http://example.com', method: 'POST', formData: { 'regularField': 'someValue', 'regularFile': someFileStream, 'customBufferFile': { value: fileBufferData, options: { filename: 'myfile.bin' } } } }, handleResponse); 

This is useful if you need to avoid calling requestObj.form() , but you need to load the buffer as a file. The form-data module also accepts contentType (MIME type) and knownLength .

This change was added in October 2014 (2 months after this question was asked), so it should be used in safe mode (in 2017+). This corresponds to version v2.46.0 or higher of request .

+6
Mar 25 '17 at 18:39
source share

Leonid Beschastny's answer works, but I also had to convert the ArrayBuffer to a buffer, which is used in the Node request module. After uploading the file to the server, I had it in the same format as in the HTML5 FileAPI file (I use Meteor). The full code is below - it may be useful for others.

 function toBuffer(ab) { var buffer = new Buffer(ab.byteLength); var view = new Uint8Array(ab); for (var i = 0; i < buffer.length; ++i) { buffer[i] = view[i]; } return buffer; } var req = request.post(url, function (err, resp, body) { if (err) { console.log('Error!'); } else { console.log('URL: ' + body); } }); var form = req.form(); form.append('file', toBuffer(file.data), { filename: file.name, contentType: file.type }); 
+4
Aug 17 '14 at 10:41
source share

you can use this:

 //toUpload is the name of the input file: <input type="file" name="toUpload"> let fileToUpload = req.file; let formData = { toUpload: { value: fs.createReadStream(path.join(__dirname, '..', '..','upload', fileToUpload.filename)), options: { filename: fileToUpload.originalname, contentType: fileToUpload.mimeType } } }; let options = { url: url, method: 'POST', formData: formData } request(options, function (err, resp, body) { if (err) cb(err); if (!err && resp.statusCode == 200) { cb(null, body); } }); 
0
Mar 25 '17 at 17:17
source share



All Articles