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
ormultipart/form-data
, then you don't show any Express middleware that knows how to parse that, so naturallyreq.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 handlesapplication/json
andapplication/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
, notapplication/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
. Fromnpm 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
andconnect-busboy
,multiparty
andconnect-multiparty
,formidable
,multer
." (their emphasis) – T.J. Crowder Commented Mar 25, 2022 at 13:48
2 Answers
Reset to default 6I 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
版权声明:本文标题:javascript - Node.js req.body undefined in form-data content-type - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741711923a2393893.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论