admin管理员组

文章数量:1302333

Here I have created the small demo for this form-data passing API. Now I'm checking this API using postman but I'm not getting any data.

Code

const http = require("http");
const express = require("express");
const bodyParser = require("body-parser");

const app = express();

app.use(
  bodyParser.json({
    limit: "50mb"
  })
);

app.use(
  bodyParser.urlencoded({
    limit: "50mb",
    extended: true
  })
);

app.post('/form-data', (req, res) => {
  console.log("form-data ->> ", req.body) 
});

server = http.createServer(app);

server.listen(4000[![enter image description here][1]][1], () => {
  console.log(`Server started`);
});

Server log

Server started
form-data ->> {}

Header

Here I have created the small demo for this form-data passing API. Now I'm checking this API using postman but I'm not getting any data.

Code

const http = require("http");
const express = require("express");
const bodyParser = require("body-parser");

const app = express();

app.use(
  bodyParser.json({
    limit: "50mb"
  })
);

app.use(
  bodyParser.urlencoded({
    limit: "50mb",
    extended: true
  })
);

app.post('/form-data', (req, res) => {
  console.log("form-data ->> ", req.body) 
});

server = http.createServer(app);

server.listen(4000[![enter image description here][1]][1], () => {
  console.log(`Server started`);
});

Server log

Server started
form-data ->> {}

Header

Share edited Mar 25, 2022 at 13:52 Jay Bhajiyawala asked Mar 25, 2022 at 13:16 Jay BhajiyawalaJay Bhajiyawala 1252 gold badges5 silver badges12 bronze badges 5
  • What headers did you include in your POST? – T.J. Crowder Commented Mar 25, 2022 at 13:26
  • 1 If the content-type is indeed form-data or multipart/form-data, then you don't show any Express middleware that knows how to parse that, so naturally req.body will be empty because nothing read or parsed the body. If you have to use this content-type, then you will want to get some middleware such as multer that can read and parse that content-type. Right now, your server code only handles application/json and application/x-www-form-urlencoded content-types and your post has neither of those. – jfriend00 Commented Mar 25, 2022 at 13:36
  • @T.J.Crowder I have set Content-Type:application/x-www-form-urlencoded in request header. – Jay Bhajiyawala Commented Mar 25, 2022 at 13:41
  • 1 Your screenshot from Postman shows multipart/form-data, not application/x-www-form-urlencoded. – jfriend00 Commented Mar 25, 2022 at 13:42
  • 1 @JayBhajiyawala - You'll need to add middleware that supports multipart/form-data. From npm body-parser: "This does not handle multipart bodies, due to their plex and typically large nature. For multipart bodies, you may be interested in the following modules: busboy and connect-busboy, multiparty and connect-multiparty, formidable, multer." (their emphasis) – T.J. Crowder Commented Mar 25, 2022 at 13:48
Add a ment  | 

2 Answers 2

Reset to default 6

I tried to reproduce your code with small changes.

const express = require("express");
const bodyParser = require("body-parser");
var multer = require("multer");
var upload = multer();

const app = express();

// for parsing application/json
app.use(
    bodyParser.json({
        limit: "50mb",
    })
);
// for parsing application/xwww-form-urlencoded
app.use(
    bodyParser.urlencoded({
        limit: "50mb",
        extended: true,
    })
);

// for parsing multipart/form-data
app.use(upload.array());

app.post("/form-data", (req, res) => {
    console.log(`\nform-data ->> ${JSON.stringify(req.body)}`);
    res.send(req.body);
});

const port = 3000;
app.listen(port, () => {
    console.log(`Example app listening on port ${port}`);
});

I removed your server initialization since we can use app listen directly from the expressjs.

And I can send post with "form-data", "x-www-form-urlencoded", or "raw" successfully.

You might double-check on which tutorial you following. Since express documentation is clear enough.

*Edited I added multer to parsing the form-data.

if you are using multer as middleware on the post route with other middle middlewares put the multer middleware first.

app.post(
    "/api/private/buyer/add-buyer",
    [
        upload, verifySignUp.checkDuplicateUsernameOrEmail,
    ],
    controller.createBuyer
);

here upload is multer middleware I put it first it works for me.

本文标签: javascriptNodejs reqbody undefined in formdata contenttypeStack Overflow