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

后端 未结 6 1796
醉酒成梦
醉酒成梦 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: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.).)

提交回复
热议问题