Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swagger generated Nodejs : Error: request entity too large

Generated Nodejs code using editor.swagger and in body I want to send 10mb but unable to send, I'm getting this error:

Error: request entity too large
    at makeError (c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\node_modules\body-parser\node_modules\raw-body\index.js:154:15)
    at readStream (c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\node_modules\body-parser\node_modules\raw-body\index.js:188:15)
    at getRawBody (c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\node_modules\body-parser\node_modules\raw-body\index.js:95:12)
    at read (c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\node_modules\body-parser\lib\read.js:64:3)
    at jsonParser (c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\node_modules\body-parser\lib\types\json.js:116:5)
    at c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\middleware\swagger-metadata.js:72:9
    at urlencodedParser (c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\node_modules\body-parser\lib\types\urlencoded.js:91:37)
    at bodyParser (c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\middleware\swagger-metadata.js:68:5)
    at c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\middleware\swagger-metadata.js:258:5
    at c:\apigee\dojo\nodeServer_2\node\node_modules\swagger-tools\node_modules\async\lib\async.js:356:13

I already tried this option but still same error. Here is a sample code from index.js:

swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
  // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
     app.use(middleware.swaggerMetadata());
  /*app.use(middleware.swaggerMetadata(
      bodyParser.json({limit: "50mb"}),
      bodyParser.urlencoded({ limit: "50mb", extended: true })
  )
  );*/

  // Validate Swagger requests
  app.use(middleware.swaggerValidator());

  // Route validated requests to appropriate controller
  app.use(middleware.swaggerRouter(options));

  // Serve the Swagger documents and Swagger UI
  app.use(middleware.swaggerUi());

  //app.use(bodyParser.json({limit: "50mb"}));
  //app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));

  app.use(bodyParser.json({limit: 10 * 1024 * 1024}));
  app.use(bodyParser.raw({limit: 10 * 1024 * 1024}));
  app.use(bodyParser.text({limit: 10 * 1024 * 1024}));
  app.use(bodyParser.urlencoded({limit: 10 * 1024 * 1024, extended: true}));
like image 907
Amir Avatar asked Dec 12 '15 00:12

Amir


1 Answers

According to instructions specified in https://github.com/apigee-127/swagger-tools/issues/248, you want to configure bodyParser before initializing swagger, like this:

app.use(bodyParser.json({limit: 10 * 1024 * 1024}));
app.use(bodyParser.raw({limit: 10 * 1024 * 1024}));

app.use(require("./router")); // in this sample, this is where you configure your Swagger middleware

This way, Swagger will use your configured bodyParser with the limits you set. I was solving the same problem and this solved it.

like image 197
Tomas Kohl Avatar answered Nov 07 '22 15:11

Tomas Kohl