PayloadTooLargeError: request entity too large

32,369

Solution 1

My issue was because I had app.use(express.json()) and also

app.use(bodyParser.json({ limit: "50mb" }))

and

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

I solved it after removing app.use(express.json()). Hope it helps somebody with the same issue.

Solution 2

Controlling the maximum request body size will do the trick, however, you do not need body-parser anymore. Instead of using body-parser middleware, use the new Express implementation:

app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb', extended: true, parameterLimit: 50000}));

You can find here the complete documentation.

Share:
32,369

Related videos on Youtube

ya90net
Author by

ya90net

Updated on July 09, 2022

Comments

  • ya90net
    ya90net almost 2 years

    I got the following error with bodyParser:

    PayloadTooLargeError: request entity too large
    at readStream (/root/server/node_modules/raw-body/index.js:155:17)
    at getRawBody (/root/server/node_modules/raw-body/index.js:108:12)
    at read (/root/server/node_modules/body-parser/lib/read.js:77:3)
    at urlencodedParser (/root/server/node_modules/body-parser/lib/types/urlencoded.js:116:5)
    at Layer.handle [as handle_request] (/root/server/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/root/server/node_modules/express/lib/router/index.js:317:13)
    at /root/server/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/root/server/node_modules/express/lib/router/index.js:335:12)
    at next (/root/server/node_modules/express/lib/router/index.js:275:10)
    at jsonParser (/root/server/node_modules/body-parser/lib/types/json.js:118:7)
    at Layer.handle [as handle_request] (/root/server/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/root/server/node_modules/express/lib/router/index.js:317:13)
    at /root/server/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/root/server/node_modules/express/lib/router/index.js:335:12)
    at next (/root/server/node_modules/express/lib/router/index.js:275:10)
    at initialize (/root/server/node_modules/passport/lib/middleware/initialize.js:53:5)
    at Layer.handle [as handle_request] (/root/server/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/root/server/node_modules/express/lib/router/index.js:317:13)
    at /root/server/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/root/server/node_modules/express/lib/router/index.js:335:12)
    at next (/root/server/node_modules/express/lib/router/index.js:275:10)
    at jsonParser (/root/server/node_modules/body-parser/lib/types/json.js:118:7)
    

    the code is following

    const express = require("express");
    const bodyParser = require('body-parser');
    
    function setEntity(req, res) {
       // something....
    }
    
    module.exports = (app) => {
    
      const router = new express.Router();
    
      app.use(bodyParser.json({limit:'50mb'}));
      app.use(bodyParser.urlencoded({
        extended: true
      }));
    
      router.use('/set/', (req, res) => {
        setEntity(req, res);
      });
    
      return router;
    };
    

    It seems similar to this question

    So I tried these 3 solutions.

    1.

    app.use(bodyParser.json({limit: '50mb'}));
    app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
    

    2.

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

    3.

    app.use(bodyParser.json({limit:1024*1024*20, type:'application/json'}));
    app.use(bodyParser.urlencoded({extended:true,limit:1024*1024*20,type:'application/x-www-form-urlencoding' }));
    

    but the result didn't change.

    another setting is like below

    nginx has this setting : client_max_body_size 200M;
    data size is under 500kb (json).
    express : 4.15.3
    body-parser : 1.18.2
    

    I have no idea why I can't change limit data size.

    • ya90net
      ya90net almost 6 years
      I found app.use(bodyParser.json()); on another file. And I could solve this problem with this line. Thanks.
  • Luke_
    Luke_ about 3 years
    Hi, welcome to stackoverflow. Great you found your answer! next time put your code in a code block, this makes things easier to read
  • SefaUn
    SefaUn over 2 years
    first situation solved my problem. thanks :)
  • lmat - Reinstate Monica
    lmat - Reinstate Monica over 2 years
    I have moved away from bodyParser, but there are little holes like this that need filling. Thank you for helping fill the gaps!