Juan Pablo Fernandez
Juan Pablo Fernandez

Reputation: 2546

How to increase default maximum request body size in loopback 4 framework?

How to increase default maximum request body size in loopback 4 framework? I understand express is used internally by loopback 4, what I need to do is the equivalent of setting the limit param for the body-parser expressjs middleware.

Any ideas?

Thanks

Upvotes: 3

Views: 2388

Answers (2)

Jonathan Declan Tan
Jonathan Declan Tan

Reputation: 151

I updated index.ts to add rest request body parser configuration to the application options variable:

Increase limit to 6MB shown below:

import {ApiServerApplication} from './application';
import {ApplicationConfig} from '@loopback/core';

export {ApiServerApplication};

export async function main(options: ApplicationConfig = {}) {
  options.rest = {requestBodyParser: {json: {limit: '6MB'}}};
  const app = new ApiServerApplication(options);
  await app.boot();
  await app.start();

  const url = app.restServer.url;
  console.log(`Server is running at ${url}`);

  return app;
}

Upvotes: 2

Juan Pablo Fernandez
Juan Pablo Fernandez

Reputation: 2546

server.bind(RestBindings.REQUEST_BODY_PARSER_OPTIONS).to({
  limit: '4MB',
});

or

server.bind(RestBindings.REQUEST_BODY_PARSER_OPTIONS).to({
  json: {limit: '4MB'},
  text: {limit: '1MB'},
});

The list of options can be found in the body-parser module.

By default, the limit is 1MB. Any request with a body length exceeding the limit will be rejected with http status code 413 (request entity too large).

Upvotes: 1

Related Questions