Swagger-Autogen and Multer: Multiform data

See original GitHub issue

Hi Davi,

I have an endpoint used for file uploading. I’m using express for the router and multer as a middleware for the upload task. My upload endpoint should receive two files. Multer inserts the files in req. files. How can I set up swagger to recognize it?

Here is what I am trying to do.

const controller = (req, res) => {
  // #swagger.tags = ['Reconcile']
  // #swagger.summary = 'Your summary here'
  // #swagger.description = 'My description'
  // #swagger.consumes = ['multipart/form-data']
  // #swagger.produces = ['application/json']
  /*  #swagger.parameters['file_A'] = {
        in: 'files',
        type: 'file',
        required: 'true',
        description: 'File data',
  } */
  /*  #swagger.parameters['file_B'] = {
        in: 'files',
        type: 'file',
        required: 'true',
        description: 'File data',
  } */

  const files = req.files;
  console.log(files);
}
image

Thanks, Paulo

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
PauloFaverocommented, Jun 27, 2021

Thanks Davi!

It works! I will close the issue.

2reactions
davibaltarcommented, Jun 27, 2021

Thanks for the code, @PauloFavero. Trying here…

in route.js file:

router.post("/test", uploader.array("transactions", 2), (req, res) => {
  // #swagger.tags = ['Test']
  // #swagger.summary = 'Test Endpoints'
  // #swagger.description = 'Upload two csv files.'
  // #swagger.consumes = ['multipart/form-data']
  // #swagger.produces = ['application/json']
  /*  #swagger.parameters['transactions'] = {
          in: 'formData',
          type: 'array',
          required: true,
          description: 'Transactions file',
          collectionFormat: 'multi',
          items: { type: 'file' }
  } */

  const files = req.files;

  console.log(files);
  console.log(req);

  try {
    console.log(files.map((file) => file));
    res.status(200).json({ message: "File Parsed with success" });
  } catch (error) {
    console.log(error);
    res.status(500).send({ error: error.message });
  }
});

here, it work fine! The req.files received the 2 files. Try it and let me know if doesn’t work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

using swagger (v3; yaml) multiparty form-data and node.js ...
parser(req) seems fails when using swagger api. The reason seems to be because the data is located in a different location within the...
Read more >
swagger-autogen - npm
This module performs the automatic construction of the Swagger documentation. The module can identify the endpoints and automatically ...
Read more >
Multipart form data file upload runs into size limit issue #338
I have this file upload api (multipart/form-data) and I run into a http 413 when I upload a 3.2 mb ... Swagger-node uses...
Read more >
File Upload - Swagger
Swagger 2.0 supports file uploads sent with Content-Type: multipart/form-data . That is, your API server must consume multipart/form-data for this operation:.
Read more >
Complete News/Blog API using NodeJS (Express), MongoDB ...
Data modeling and schema construction using Mongoose; Categories, stories, videos and comments CRUD and authorized or protected routes; Swagger ...
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