【问题标题】:Express: PayloadTooLargeError: request entity too largeExpress: PayloadTooLargeError: 请求实体太大
【发布时间】:2020-05-01 06:08:40
【问题描述】:

当我尝试在 POST 请求中发送 Base64 字符串时出现此错误。

POST /saveImage 413 10.564 ms - 1459
PayloadTooLargeError: request entity too large

已经试过了

-->  app.use(bodyParser.urlencoded({ limit: "50mb", extended: true, parameterLimit: 50000 }))
-->  app.use(bodyParser.urlencoded({limit: '50mb'}));
-->  app.use(bodyParser({limit: '50mb'}));

这是我的代码(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(图片类包含Base64格式图片等字段,但我猜是因为Base64字符串长度问题,小图片不会造成任何问题)

create(image: Image) {
return this._http.post('/saveImage', image)
  .map(data => data.json()).toPromise()
 }

【问题讨论】:

    标签: javascript node.js express base64 tedious


    【解决方案1】:

    我遇到了同样的错误。我试过了,你试过了,还是不行。

    我猜你正在上传一个文件。解决这个问题的简单方法是不设置 Content-Type。

    我的问题是我设置了我的标题:Content-Type: application/json,而我正在使用 multer(用于上传文件的 expressjs 中间)。

    每当我尝试上传文件时都会出现错误。

    因此,当使用 postman 或使用任何工具或库(如 axiosjs 或 fetch() API)发出此类请求时,请不要设置 content-type。

    删除 Content-type 后,它将起作用。我就是这么做的

    在我的代码中,我有:

    const express = require('express');
    
    ...
    
    const app = express();
    
    app.use(express.json());
    ...
    

    ...它正在工作,因为我删除了我的邮递员标题上的 Content-Type。

    确保您没有在标题上使用 Content-Type。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-22
      • 2019-03-17
      • 2019-11-27
      • 2022-11-27
      • 2018-06-10
      • 2021-12-16
      • 2020-01-25
      • 2013-03-15
      相关资源
      最近更新 更多