[英]Express: PayloadTooLargeError: request entity too large
I'm getting this error when I try to send a Base64 string in a POST request.当我尝试在 POST 请求中发送 Base64 字符串时出现此错误。
POST /saveImage 413 10.564 ms - 1459
PayloadTooLargeError: request entity too large
Already tried已经试过了
--> app.use(bodyParser.urlencoded({ limit: "50mb", extended: true, parameterLimit: 50000 }))
--> app.use(bodyParser.urlencoded({limit: '50mb'}));
--> app.use(bodyParser({limit: '50mb'}));
Here's my code (api.js class)这是我的代码(api.js 类)
const express = require('express');
var app = express();
const router = express.Router();
var Connection = require('tedious').Connection
var Request = require('tedious').Request
var TYPES = require('tedious').TYPES
var multer = require('multer');
....
....
....
router.post('/saveImage', (req, res) => {
request=new Request('SAVE_IMAGE',(err, rowCount, rows)=>{
if(err){
console.log(err);
}
});
request.addParameter("Base64Image", TYPES.Text, req.body.IMG)
connection.callProcedure(request);
});
API CALL (Image class contains a Base64 format image and other fields, but I guess the problem occurs because of the Base64 string length. Small images don't cause any trouble) API CALL (Image class 包含一个 Base64 格式的图像和其他字段,但我猜问题是因为 Base64 字符串长度。小图像不会造成任何麻烦)
create(image: Image) {
return this._http.post('/saveImage', image)
.map(data => data.json()).toPromise()
}
I was having the same error.我有同样的错误。 I tried what you tried it did not work.
我尝试了您尝试的方法,但没有成功。
I guess you are uploading a file.我猜你正在上传一个文件。 The simple way to solve this is to not set a Content-Type.
解决这个问题的简单方法是不设置 Content-Type。
my problem was that I was setting on my headers: Content-Type: application/json
and I am [was] using multer (expressjs middle for uploading files).我的问题是我正在设置我的标题:
Content-Type: application/json
并且我正在使用 multer (用于上传文件的 expressjs 中间)。
I have the error whenever I try uploading a file.每当我尝试上传文件时都会出现错误。
So when using postman or making such requests using any tools or libraries like axiosjs or fetch() API do not set content-type.因此,当使用 postman 或使用任何工具或库(如 axiosjs 或 fetch() API )发出此类请求时,请勿设置内容类型。
Once you remove the Content-type it will work.删除 Content-type 后,它将起作用。 That is what I did
这就是我所做的
on my code, I have:在我的代码中,我有:
const express = require('express');
...
const app = express();
app.use(express.json());
...
...And it is working because I removed Content-Type on my postman headers. ...它正在工作,因为我删除了 postman 标头上的 Content-Type。
Make sure you are not using Content-Type on the headers.确保您没有在标题上使用 Content-Type。
I would recommend you to use express
instead of body-parser
, as body-parser
got merged back in express
a long ago.我建议您使用
express
而不是body-parser
,因为body-parser
很久以前就合并回了express
。
I am using this code and it seems to work fine, setting the limit
option to 200mb, of both express.json
and express.urlencoded
我正在使用这段代码,它似乎工作正常,将
express.json
和express.urlencoded
的limit
选项设置为 200mb
app.use(express.json({ limit: "200mb" }));
app.use(express.urlencoded({ extended: true, limit: "200mb" }));
Source: express.json vs bodyparser.json来源: express.json vs bodyparser.json
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.