10

I'm creating rest endpoints in my nodejs application as follows:

In my server.js I have the following code:

var express = require('express');
var app = express();
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));
app.use(require('./routes/APIRoutes'));

I also tried the code below instead of using the express.json and expres.urlencoded as suggested on the potential duplicate question.

var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
app.use(require('./routes/APIRoutes'));

In my APIRoutes file I have the following code:

/* --- INIT --- */
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
router.use(bodyParser.json({ limit: '50mb' }));
router.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));

var urlencodedParser = bodyParser.urlencoded({ limit: '50mb', extended: true });

router.post('...', urlencodedParser, function (req, res) {
...
});

I tried different combinations and orders for setting the limit higher. But currently every time I send a payload of 2kb, I get an 413 "(Payload Too Large)" error back.

Any help what the correct location is for setting the limit?

Stephan Celis
  • 2,266
  • 4
  • 20
  • 37

5 Answers5

19

I found the solution for my problem in one of the unaccepted answers of the suggested duplicate of TKJohn. I had to add the parameter limit so that my code became:

var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true, parameterLimit: 50000 }));

After the addition it worked!

I was also ablo to remove the code from APIRoutes.js

router.use(bodyParser.json({ limit: '50mb' }));
router.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
Stephan Celis
  • 2,266
  • 4
  • 20
  • 37
4

You can use this straightaway in express if you are using the latest version of express.

    let express = require('express');

    let app = express();

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

Please note that this will only work with express versions bundled with bodyParser (bodyParser was added back to express in release 4.16.0)

Sudheesh R
  • 1,624
  • 2
  • 17
  • 39
2

This worked for me

this.express.use(bodyParser.json({ limit: '50mb' }));
this.express.use(bodyParser.urlencoded({ limit: "50mb" ,extended: true, parameterLimit: 50000 }));
Arghya Sadhu
  • 28,262
  • 9
  • 35
  • 59
1

Tried looking online for past couple of hours and found no solution to 413 Payload

Turns out in urlencoded.js within the body-parser module there are additional options which take care of limits.

Options like parameterLimit and arrayLimit are items to amend if your parameter or array size exceeds the limit that is granted by default (which is 100 for array size).

0

I was having the same error. I have tried all those suggestions or solutions but failed to work.

Are you uploading file?

In my case, I was using multer and also was uploading files with it.

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: so my app was trying to parse the uploaded audio file to JSON when the solution:

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

...is used and or the server complaints of large payload when it is removed.

Currently, my code is working

on my code, I have just this with the above solution that was failing base on my app case (file upload):

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 when sending the request. If you care to know I was not setting it either on the express app(API).