'413 payload too large, for base64 string after adjusting size in express
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}));
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}));
Solution 1:[1]
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' }));
Solution 2:[2]
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.
Solution 3:[3]
It beacuse you called the same bodyParser.[method] twice. Never do that. Refer this question - duplicate of this
Solution 4:[4]
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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | RONAK SUTARIYA |
Solution 2 | noszone |
Solution 3 | Nikhil Unni |
Solution 4 | julioalberto64 |