Home > Software engineering >  I am getting payload too large error when uploading an image in base64 format to database
I am getting payload too large error when uploading an image in base64 format to database

Time:02-02

I am trying to upload a Base64 file to a MongoDB database but axios is giving me an error like "payload too large"

r.js:247          POST http://localhost:5000/api/auth/signup 413 (Payload Too Large)

The Base64 file size is like 400kb

I have tried all the solution like below:

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

But this is not working.

CodePudding user response:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
const port = process.env.PORT || 4000;

app.use(express.json({ limit: '50mb' }));

app.use('/api/v1', require("./routes/v1/index.js"));


app.listen(port, () => {
   console.log(`app listening on port ${port}`);
})

Here the sequence matters. After some research and testing, I found that when debugging, I added app.use(express.bodyParser({limit: '50mb'}));, but after app.use(express.json());. Express would then set the global limit to 1mb because the first parser he encountered when running the script was express.json(). Moving bodyParser above it did the trick.

  • Related