Node.js (with express & bodyParser): unable to obtain form-data from post request

后端 未结 6 1773
醉酒成梦
醉酒成梦 2020-11-28 11:35

I can\'t seem to recover the form-data of a post request sent to my Node.js server. I\'ve put below the server code and the post request (sent using postman in chrome):

相关标签:
6条回答
  • 2020-11-28 12:14

    I followed this https://www.tutorialspoint.com/expressjs/expressjs_form_data.htm

    var bodyParser = require('body-parser');
    var multer = require('multer');
    var forms = multer();
    
    // apply them
    
    app.use(bodyParser.json());
    app.use(forms.array()); 
    app.use(bodyParser.urlencoded({ extended: true }));
    
    // how to use
    
    router.post('/', function(req, res) {
        console.log(req.body);
        console.log('received the widget request');
    });
    
    0 讨论(0)
  • 2020-11-28 12:23

    Make sure to put in this order: bodyParser.json() first. app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true }));

    0 讨论(0)
  • 2020-11-28 12:25
    • For Json: Use body-parser.
    app.use(bodyParser.json())
    app.use(bodyParser.urlencoded({extended: true}))
    

    (you should Also send Content-Type: application/json in request header)

    • For Normal Form, Or multipart form (form with files), Use body-parser + multer.
    app.use(bodyParser.json())
    app.use(bodyParser.urlencoded({extended: true}))
    app.use(multer().array())
    

    (You should NOT send Content-Type: application/json in this case. you should send nothing, or Content-Type: multipart/form-data if you have files in form.

    • in postman you should not send Content-Type: multipart/form-data manually. otherwise you'll get an error (Boundary not found). (it will add this automatically.).)
    0 讨论(0)
  • 2020-11-28 12:27

    In general, an express app needs to specify the appropriate body-parser middleware in order for req.body to contain the body.

    [EDITED]

    1. If you required parsing of url-encoded (non-multipart) form data, as well as JSON, try adding:

      // Put this statement near the top of your module
      var bodyParser = require('body-parser');
      
      
      // Put these statements before you define any routes.
      app.use(bodyParser.urlencoded());
      app.use(bodyParser.json());
      

      First, you'll need to add body-parser to the dependencies property of your package.json, and then perform a npm update.

    2. To handle multi-part form data, the bodyParser.urlencoded() body parser will not work. See the suggested modules here for parsing multipart bodies.

    0 讨论(0)
  • 2020-11-28 12:29

    Make sure you are not sing enctype as multipart/form-data, body parser does not support it. use below line before you define any route.

    app.use(bodyParser.urlencoded()); app.use(bodyParser.json());

    0 讨论(0)
  • 2020-11-28 12:33

    To handle multipart/form-data request that support file upload, you need to use multer module. npm link for multer middleware

    0 讨论(0)
提交回复
热议问题