注意:仅当我在 Postman 中使用表单数据正文表单(这是我必须使用的表单,因为我想在文本字段旁边发送文件)时,我得到:
Error: Multipart: Boundary not found
。
当我使用 x-www-form-urlencoded 时,一切正常。 ( 当然当 body-parser 用作中间件时)
这是请求内容:(由 Postman 制作)
POST /test HTTP/1.1
Host: localhost:3000
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: 61089a44-0b87-0530-ca03-a24374786bd1
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="test"
a simple word
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="data"
good
------WebKitFormBoundary7MA4YWxkTrZu0gW--
index.js:
var express = require('express');
var app = express();
var multer = require('multer');
var upload = multer();
app.post('/test', upload.array(), function (req, res, next) {
console.log(req.body.test);
console.log(req.body);
});
app.listen(3000, function () {
console.log('app started');
});
原文由 user9150719 发布,翻译遵循 CC BY-SA 4.0 许可协议
我找到了解决方案。我只需要 阻止邮递员发送
Content-Type
标头。所以我只是从请求标头中删除了它。