4

I have tried to make a post request, to my node server, with a base64 encoded file.

I get a PayloadTooLargeError: request entity too large exception, so i went to extend the payload limit, by Express 4 convention

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

Here is a console.log of the picture

however the problem still occurs, can anybody help me on why?

here are my global variables

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false}))
app.use(bodyParser.json({limit: '100mb'}));
app.use(bodyParser.urlencoded({limit: '100mb', extended: true}));
worc
  • 3,528
  • 3
  • 29
  • 34
Dedi
  • 2,812
  • 3
  • 22
  • 55
  • Can you share a pastebin link with the string? – ninesalt Feb 15 '19 at 20:55
  • It's to large for a pastebin – Dedi Feb 15 '19 at 21:01
  • a couple of questions stand out, the base64 string is definitely under 100MB? and is the bodyParser limit case-sensitive? 100 mega**b**its would be significantly smaller than 100 mega**B**ytes – worc Feb 15 '19 at 21:11
  • depending on the kind of error message you're getting, it might be another part of your stack: https://stackoverflow.com/questions/19917401/error-request-entity-too-large/40745569#40745569 – worc Feb 15 '19 at 21:24
  • Got exaclty the same problem. The payload limit works when you upload files. But not on large strings like this. – Joe Apr 13 '22 at 17:19
  • @Dedi are you using nginx? – bill.gates Apr 18 '22 at 07:22

4 Answers4

2

In your code you have called twice to bodyParser.json() & bodyParser.urlencoded(), the problem is that the one with limit is after the calls without options, which defaults to 100kb. So when you POST something greater than 100kb that error will be thrown, since the first parser will not go to the next middleware.

  • You shouldn't call bodyParser[method] twice.

  • Depending on how you're posting the base64 string, you may want to use

    app.use(myParser.text({ limit: '200mb' }));

RONAK SUTARIYA
  • 526
  • 3
  • 10
1

In your code:

app.use(bodyParser.json())

Should be not used without options. If no options, the default size I believe will be about 100 Kb.

You can pay attention to text parameter below:

app.use(myParser.text({ limit: '200mb' }));

If you are using a last version of express, so it should be something like this:

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

Try adding also a flag parameterLimit > 100000 or larger.

noszone
  • 100
  • 7
1

It beacuse you called the same bodyParser.[method] twice. Never do that. Refer this question - duplicate of this

Nikhil Unni
  • 689
  • 5
  • 12
0

This is worked for me set the 'type' in addition to the 'limit' for bodyparser

var app = express(); 
var jsonParser = bodyParser.json({limit:1024*1024*10, type:'application/json'}); 
var urlencodedParser = bodyParser.urlencoded({ extended:true,limit:1024*1024*10,type:'application/x-www-form-urlencoded' });
app.use(jsonParser);
app.use(urlencodedParser);