1

I have a page where user uploads files and are passed to Node API which does some operations like database insert and finally has to send the files to another node rest API for virus check.

I can see file content and attributes in req.files in the first API call but struggling to pass req.files to another node API URL using request.post.

 var uploadFile= function(req, res) {
       var files = req.files.upload;
Async.series(
        [ function (callback) {..},
          function (callback){viruscheck(files,callback);}
....

//viruscheck method
var viruscheck= function(files, callback) {
request.post({url: "http://loclahost:1980/viruscheck/upload.json", 

                qs: {
                    ..
                }, 
                headers: {'enctype-type': 'multipart/form-data'},
                form:{
                    upload:files
                }
        },function(error, response, body){
            if(error) {
                console.log(error);
                callback(error)
            } else {
                console.log(response.statusCode, body);
                callback()
            }
        });
}

In API http://loclahost:1980/viruscheck/upload.json I see file content in body rather than req.files.

How can I get the files in request.files in my second API call?

Ram
  • 3,092
  • 10
  • 40
  • 56
Sam
  • 11
  • 1
  • 2

1 Answers1

2

Use:

headers: {'Content-Type': 'multipart/form-data'},

Instead of:

headers: {'enctype-type': 'multipart/form-data'},

enctype is an HTML form thing.

See this answer for more: Uploading file using POST request in Node.js

Community
  • 1
  • 1
  • I have tried using content-type but still I see file contents in body . Here is the request object conetnts on viruscheck server body:{upload[]..} _body: true, length: undefined, read: [Function], files: {}, route: Route { path: '/upload(.:ext)', stack: [ [Object] ], methods: { post: true } } } – Sam Oct 30 '15 at 17:50
  • You're setting var files equal to req.files.upload. What is this? A buffer? Should it be set to req.files instead? – Matthew Rayfield Oct 30 '15 at 18:01
  • It is buffer. As suggested I tried with req.files, header is also set 'contet-type':'multipart/form-data' but I still see the files in request body rather than req.files. – Sam Oct 30 '15 at 18:13