简体   繁体   English

从 postman 发送 application/x-www-form-urlencoded 时读取 req.body 内容的问题 — bodyparser 已使用

[英]Issue reading req.body content when sending application/x-www-form-urlencoded from postman — bodyparser already used

Like the title says.正如标题所说。 I am sending multipart form data from postman with a few text fields as well as an image.我正在从 postman 发送多部分表单数据,其中包含一些文本字段和图像。 However when I console.log(req.body) I get但是,当我 console.log(req.body) 我得到

{
  '------WebKitFormBoundaryCpNHg1P01X54QAVr\r\nContent-Disposition: form-data; name': '"customerID"\r\n' +
    '\r\n' +
    '1\r\n' +
    '------WebKitFormBoundaryCpNHg1P01X54QAVr\r\n' +
    'Content-Disposition: form-data; name="make"\r\n' +
    '\r\n' +
    'honda\r\n' +
    '------WebKitFormBoundaryCpNHg1P01X54QAVr\r\n' +
    'Content-Disposition: form-data; name="model"\r\n' +
    '\r\n' +
    'accord\r\n' +
    '------WebKitFormBoundaryCpNHg1P01X54QAVr\r\n' +
    'Content-Disposition: form-data; name="year"\r\n' +
    '\r\n' +
    '2020\r\n' +
    '------WebKitFormBoundaryCpNHg1P01X54QAVr\r\n' +
    'Content-Disposition: form-data; name="color"\r\n' +
    '\r\n' +
    'white\r\n' +
    '------WebKitFormBoundaryCpNHg1P01X54QAVr--\r\n'
}

If I console.log(req.body.keyName) i always have undefined.如果我 console.log(req.body.keyName) 我总是有未定义的。 I have already implemented what similar posts have suggested ie using我已经实现了类似帖子的建议,即使用

app.use(bodyParser.urlencoded({
 extended: true
}));

But it does not work still.但它仍然不起作用。 I am not sure what I am doing wrong.我不确定我做错了什么。 Code snippets follow.代码片段如下。

server.js服务器.js

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
 extended: true
}));

app.use(cors());

app.use('/users', userRouter);
app.use('/offers', offerRouter);


app.listen(PORT, (err)=>
{
    if (err) console.log(err);
    else console.log(`Server listening on port ${PORT}`);
});

route路线


const multer =  require('multer');

//required for mulitpart-formdata
const diskStorage = multer.diskStorage({
    destination: "uploads/",
    filename: (req, file, call_back) => {
      //Prepend date to the filename or anything that makes
      //the file unique so it won't be overwritten

      call_back(null, Date.now() + "_" + file.originalname);
    },
  });
const upload = multer({ storage: diskStorage });

router.post("/request", upload.single("file"), (req, res, next) =>
{
    GetBucket();
    console.log(req.file);
})


postman postman 在此处输入图像描述

To parse a multipart request you need Multer or BusyBoy.要解析多部分请求,您需要 Multer 或 BusyBoy。 I would suggest Multer ( https://www.npmjs.com/package/multer ) as it is simple and easy to use, here is an example using Multer我建议 Multer ( https://www.npmjs.com/package/multer ) 因为它简单易用,这里是一个使用 Multer 的例子

const Multer = require('multer');
// create multer instance
const multer = Multer({
  storage: Multer.MemoryStorage, // we want to save parsed body in RAM
  onError: function(err, next) {
      console.log(err);
      next(err);
  }
});

function myController("/multipart/request", multer.single('name'), (req, res) => {
   console.log(req.body.name);
});

Note: Use multipart request if you are sending files along with your request, otherwise you can use application/x-www-form-urlencoded or application/json which can be parsed by body parser注意:如果您与请求一起发送文件,请使用多部分请求,否则您可以使用application/x-www-form-urlencodedapplication/json可以由正文解析器解析

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在请求中解码 JSON object,其中内容类型为“application/x-www-form-urlencoded” - Decode JSON object in req where content type 'application/x-www-form-urlencoded' 使用“Content-Type”:“application/x-www-form-urlencoded”从 axios 发送帖子请求会给出 401 Unauthorized 响应 - Sending post request from axios with "Content-Type" : "application/x-www-form-urlencoded" gives an 401 Unauthorized response 如何使用“内容类型”:“ application / x-www-form-urlencoded”发出发布请求? - How to make a Post request with “content type”: “application/x-www-form-urlencoded”? 如何将Content-Type:appication / x-www-form-urlencoded更改为application / json? - How to change Content-Type: appication/x-www-form-urlencoded to application/json? 如何在 node.js 中发布内容类型 ='application/x-www-form-urlencoded' 的数据 - how to post data in node.js with content type ='application/x-www-form-urlencoded' 解析ExpressJS中的application / x-www-form-urlencoded - Parsing application/x-www-form-urlencoded in Expressjs Express + bodyParser.json()+邮递员,req.body为空 - Express + bodyParser.json() + Postman, req.body is empty NodeJS从formData x-www-form-urlencoded获取值 - NodeJS get value from formData x-www-form-urlencoded node.js正文解析器对Content-Type:x-www-form-urlencoded和Form-data JSON的错误解释 - node.js body-parser bad interpretation of Content-Type:x-www-form-urlencoded and Form-data JSON 节点js如何识别邮递员扩展客户端请求来自“表单数据”或“ x-www-form-urlencoded” - node js how to identify postman extention client request came from “form-data” or “x-www-form-urlencoded”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM