FastifyError- Request body is too large

Fastify is a Node.Js Server Side Framework written on top of JavaScript scripting language. It has by default 1MB of Request Payload size limit. Passing a large payload or even more than 1MB of payload gives Request Body is too large error. Let’s resolve the FastifyError- Request body is too large error.

Below screenshot shows the default maximum payload size limit Fastify is allowed to accept.

Source: Fastify default Body Limit

How the error looks

Following screen shows the Fastify Request body too large error.

FST_ERR_CTP_BODY_TOO_LARGE- Request body is too large

Resolve FastifyError- Request body is too large

To resolve the Request body too large error, we need to increase the default size in Fastify Server instance.

Provide the bodyLimit parameter in the Fastify create server options object.


import Fastify from 'fastify'
const fastify = Fastify({
  logger: true,

  bodyLimit: 30 * 1024 * 1024 // Default Limit set to 30MB
})


// Declare a route
fastify.get('/', function (request, reply) {
  reply.send({ hello: 'world' })
})

// Run the server!
fastify.listen({ port: 3000 }, function (err, address) {
  if (err) {
    fastify.log.error(err)
    process.exit(1)
  }
  // Server is now listening on ${address}
})

Provide the size in bytes (convert to MB by multiplying 1024 twice) to bodyLimit parameter. Following line sets the default size limit to 30MB.


bodyLimit: 30 * 1024 * 1024

Save the changes and run application. Now you can send up-to 30MB of request body without any failure.

Conclusion

Fastify is Node.Js Framework to write Server-Side apps built on top of JavaScript platform. It is recommended to use with Node.Js TypeScript project.

The article resolves a very common fastify error ie. request body is too large error. Hope it helps you resolve your issue. If you still face the problem, make sure to comment below and we will try to look into the issue.

Checkout other JavaScript articles here.

Leave a Reply

Your email address will not be published. Required fields are marked *