19

Windows
Express 4.12.4
Multer 1.0.1
Node v0.10.22

I'm trying to send a file to my node.js server using postman.

I'm attempting to follow the readme here

Here's what I'm sending with postman:

POST /ingest HTTP/1.1
Host: localhost:3000
Cache-Control: no-cache
Postman-Token: 69dd2497-2002-56ed-30a4-d662f77dc0b0
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

Here's what it looks like: enter image description here

Here's what it's hitting on node.js

var Ingest      = require('../controllers/ingest.js');
var multer      = require('multer');
var upload      = multer({ dest: 'uploads/',fileFilter:function(req,file,cb){
        console.log('file is',file)
        cb(null,true);
    }
});

module.exports = function (app) {
    app.post('/ingest', upload.single('test'), function(req, res, next) {
        console.log(req.body);
        console.log(req.file);
        Ingest.ingestData()
        .then(function (response){
            return res.status(200).json(response);
        });
    });
}

When I hit this route with postman I get {} for req.body and undefined for req.file.

What am I doing wrong?

Here's where I initialize the app that gets passed in to the route file:

var express = require('express');
var app = express();
var http = require('http');
var cfg = require('./config')();
var passport = require('passport');
var cors = require('cors');

var bodyParser = require('body-parser');

app.set('port', process.env.PORT || cfg.port);
var corsOptions = {
    origin: "*",
    allowedHeaders: ['Content-Type', 'Authorization', 'Accept', 'x-reset-token', 'x-invite-token', 'x-api-key', 'x-www-form-urlencoded'],
    credentials: true
};
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(passport.initialize());
http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

Maybe something in there is doing it?

Edit:

I've even tried

var Ingest      = require('../controllers/ingest.js');
var multer      = require('multer');
var upload      = multer({ dest: 'uploads/',fileFilter:function(req,file,cb){
        console.log('file is',file)
        cb(null,true);
    }
}).single('csv');

module.exports = function (app) {

    app.post('/ingest', function(req,res){
        upload(req, res, function(err) {
            if(err){
                console.log(err);
            }
            console.log(req.body);
            console.log(req.file);
            Ingest.ingestData()
            .then(function (response){
                return res.status(200).json(response);
            });
        });
    });

}

And that didn't help. It doesn't log anything for err

5 Answers5

17

Just remove header Content-Type:application/x-www-form-urlencoded from the postman.

enter image description here

taman neupane
  • 938
  • 9
  • 18
14

In Postman screenshot, the file field name is missing. The key should be csv since Multer accepts single file with that name.

hassansin
  • 16,918
  • 3
  • 43
  • 49
11

This would help others like me who are searching for a similar answer.

I used a curl request instead of Postman.

curl -v -F csv=@file.csv http://localhost/url

There seems to be an issue with Postman.

https://github.com/expressjs/multer/issues/317

I also have this issue when I use Postman to send file using form-data. The attribute req.file or req.files is always undefined. If anyone has used Postman and was able to retrieve the file using req.file or req.files, please comment.

mythicalcoder
  • 3,143
  • 1
  • 32
  • 42
0

Updating from Postman version 5.5.3 to version 8.0.7. solved the issue for me. Maybe related to this problem fixed in version 7.

Dashiell Rose Bark-Huss
  • 2,173
  • 3
  • 28
  • 48
-1

Multer looks for the file named 'single'. So if you add the key as 'single' in postman form-data it should work

sampath
  • 49
  • 2
  • 4