简体   繁体   English

为什么在 Node.js 中使用邮递员上传文件时请求正文为空

[英]Why request body is null in file upload with postman in Node.js

I am trying to upload file from postman to the Node.js server.我正在尝试将文件从邮递员上传到 Node.js 服务器。 I am using multer and body-parse to parse the body of the request.我正在使用 multer 和 body-parse 来解析请求的正文。

Use case:用例:

  1. when I use POST request with raw-JSON as Body in Postman, everything works fine and I can see the req.body exactly as provided in the Postman and also I can assign new key-value pair to the req.body so that these new variables can be use in further middlewares in the route.当我在 Postman 中使用带有原始 JSON 的 POST 请求作为正文时,一切正常,我可以看到与 Postman 中提供的完全相同的 req.body,并且我还可以为 req.body 分配新的键值对,以便这些新的变量可以在路由中的其他中间件中使用。
  2. But when I try to upload a file from the postman with form-data as Body in Postman, then still I can get the value from req.body as provided.但是当我尝试从邮递员上传文件时,表单数据作为邮递员中的正文,那么我仍然可以从 req.body 中获取所提供的值。 But after the multer middle layer, the newly assigned values to the req.body (from the middleware) shows corrupted like req.body becomes [Object: null prototype].但是在multer中间层之后,新分配给req.body(来自中间件)的值显示损坏,如req.body变成[Object: nullprototype]。 I can confirm that Multer is working fine but Multer is corrupting the req.body data (which is created in the route middleware itself)我可以确认 Multer 工作正常,但 Multer 正在破坏 req.body 数据(它是在路由中间件本身中创建的)

Please help to identify the meaning of req.body=[Object: null prototype]请帮忙识别req.body=[Object: nullprototype]的含义

I ran into a similar issue. 我遇到了类似的问题。 If you console.log(req.body) in the function. 如果在功能中使用console.log(req.body)。 you will notice that the body is equal to [Object: null prototype] because Multer is handling the request as the form is multipart. 您会注意到该主体等于[Object:null prototype],因为Multer正在处理请求,因为表单是多部分的。 So, if you console.log(req) object, you can find the values that can be retrieved and you can just do req.{Name_of_field} to get values. 因此,如果您使用console.log(req)对象,则可以找到可以检索的值,并且只需执行req。{Name_of_field}即可获取值。

If you need clarification please let me know, I might be able to share the code. 如果您需要澄清,请告诉我,我也许可以共享代码。

Shivam Mahajan wrote almost as it goes, but: Shivam Mahajan撰写了几乎所有内容,但:

You can access text fields not in the req.{Name_of_field} , but req.body.{Name_of_field} , as said in multer docs . 您可以访问文本字段,而不是req.{Name_of_field}中的文本字段,而是req.body.{Name_of_field}中的文本字段,如multer docs中所述。

you can use express busboy add these to your main page您可以使用 express busboy 将这些添加到您的主页

const expressBusboy = require('express-busboy');

expressBusboy.extend(app); expressBusboy.extend(app);

and this will parse your form-data这将解析您的表单数据

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

相关问题 在node.js中请求正文/文件是否为空 - Request body/file empty or not in node.js 如何使用Node.js和Postman获取并上传文件 - How to get the file and upload it using Node.js and Postman 如何使用Node.js和Postman将文件上传到cloudinary - How to upload file to cloudinary using Node.js and Postman Axios - Node.js - localhost 服务器在使用 axios 上传文件时收到 null 的 req.body - Axios - Node.js - localhost server receive a req.body of null when upload a file with axios 为什么Node.js从POST请求中获取一个空的正文? - Why is Node.js getting an empty body from a POST request? POST的Node.js / Express请求主体为空。 通过邮递员发送的POST请求 - Node.js/Express req.body of POST is empty. POST request sent through Postman 在 node.js 服务器上接受请求正文中的二进制文件 - Accept binary file in body of request on a node.js server Node.js 错误:在文件上传强大的请求中止 - Node.js Error: Request aborted in File upload formidable 如何使用needle或请求库在Node.js中上传带有PUT请求的二进制主体 - How to upload a binary body with a PUT request in Node.js using needle or request libraries 为什么我可以通过 Postman 将图像上传到我的 S3 存储桶,但不能通过 Node.js 上传图像? - Why can I upload an image to my S3 bucket through Postman, but not through Node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM