简体   繁体   中英

Express: PayloadTooLargeError: request entity too large

I'm getting this error when I try to send a Base64 string in a POST request.

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)

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)

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.

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).

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.

Once you remove the Content-type it will work. 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.

Make sure you are not using Content-Type on the headers.

I would recommend you to use express instead of body-parser , as body-parser got merged back in express a long ago.

I am using this code and it seems to work fine, setting the limit option to 200mb, of both express.json and express.urlencoded

app.use(express.json({ limit: "200mb" }));
app.use(express.urlencoded({ extended: true, limit: "200mb" }));

Source: express.json vs bodyparser.json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM