"body exceeded 1mb limit" in nextjs api routes

See original GitHub issue

What version of Next.js are you using?

11.0.1

What version of Node.js are you using?

v16.5.0

What browser are you using?

Brave

What operating system are you using?

Arch Linux

How are you deploying your application?

next

Describe the Bug

I am trying to build a react image uploader application with nextjs api routes.

The /api/upload api file:

import cloudinary from 'cloudinary'

export const uploadDir = 'cules-uploader/'

const handler = async ({ body, method }, res) => {
	try {
		if (method !== 'POST')
			return res.status(400).json({ message: 'Invalid method' })

		const cloudinaryV2 = cloudinary.v2

		cloudinaryV2.config({
			cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
			api_key: process.env.CLOUDINARY_API_KEY,
			api_secret: process.env.CLOUDINARY_API_SECRET,
		})

		const { src } = body

		const imageConfig = {
			width: 1920,
			height: 1080,
			crop: 'fit',
			quality: 80,
			folder: uploadDir,
		}

		const { public_id } = await cloudinaryV2.uploader.upload(src, imageConfig)

		return res.json({ success: true, public_id })
	} catch (e) {
		return res.status(400).send({ message: 'Error processing request' })
	}
}

export default handler

I try to send a request from my client side code with axios:

const handleUpload = async () => {
	  const { data } = await axios.post('/api/upload', {
		src: base64File,
	})
  }

It works fine if the file size is less than 1mb. But if file is more than 1mb, I get 413 error. Screenshot_20210729_155808

I also extend the body parser size limit on next.config.js file.

module.exports = {
	reactStrictMode: true,
	api: {
		bodyParser: {
			sizeLimit: '50mb',
		},
	},
}

But I still get 413 error. If I send a request with postman, then it works. Screenshot_20210729_161432

Expected Behavior

When I want to send request body more than 1mb, I don’t want to get the following 413 error. Screenshot_20210729_155808

To Reproduce

/api/upload file:

import cloudinary from 'cloudinary'

export const uploadDir = 'cules-uploader/'

const handler = async ({ body, method }, res) => {
	try {
		if (method !== 'POST')
			return res.status(400).json({ message: 'Invalid method' })

		const cloudinaryV2 = cloudinary.v2

		cloudinaryV2.config({
			cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
			api_key: process.env.CLOUDINARY_API_KEY,
			api_secret: process.env.CLOUDINARY_API_SECRET,
		})

		const { src } = body

		const imageConfig = {
			width: 1920,
			height: 1080,
			crop: 'fit',
			quality: 80,
			folder: uploadDir,
		}

		const { public_id } = await cloudinaryV2.uploader.upload(src, imageConfig)

		return res.json({ success: true, public_id })
	} catch (e) {
		return res.status(400).send({ message: 'Error processing request' })
	}
}

export default handler

Client side code:

const handleUpload = async () => {
	  const { data } = await axios.post('/api/upload', {
		src: base64File,
	})
  }

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Veshal-Maniamcommented, Nov 9, 2021

Who can explain me how can i add the config? I loaded the config file in the API function file, but doesn’t work. What more i need to do?

image

Paste the following code before the handler function.

export const config = { api: { bodyParser: { sizeLimit: '25mb' // Set desired value here } } }

Read more comments on GitHub >

github_iconTop Results From Across the Web

Body exceeded 1mb limit error in Next.js API route
The default size limit for the body parser is 1mb in API routes. You can modify this value through the custom config object...
Read more >
Serverless "Body exceeded 1mb limit" #19684 - vercel/next.js
When sending FormData larger than 1MB in the body of POST request to /api serverless function I'm getting "Body exceeded 1mb limit".
Read more >
API Routes Response Size Limited to 4MB - Next.js
API Routes are meant to respond quickly and are not intended to support responding with large amounts of data. The maximum size of...
Read more >
My Next.js API Routes Were Slow, Until I ... - YouTube
Next.js api routes can be made much faster with this easy trick. The trick is to convert the api route to use edge...
Read more >
Upload Files to Next.js With API Routes | by Robert S (codeBelt)
A brief overview how to upload files with React and Next.js API Routes. How to use React, ... By default, Next.js automatically parses...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found