question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

File upload with Node

See original GitHub issue

I’m having a hard time figuring out how to create a file upload example using our swagger docs. It works fine with our Swagger UI.

The API declaration basically looks like:

{
    "apiVersion": "3",
    "swaggerVersion": "1.2",
    "basePath": "/3",
    "resourcePath": "/files.{format}",
    "apis": [
        {
            "path": "/files.{format}",
            "operations": [
                {
                    "method": "POST",
                    "summary": "Upload a JPG file (up to 100MB)",
                    "nickname": "uploadFile",
                    "parameters": [
                        {
                            "name": "File",
                            "description": "The file",
                            "paramType": "body",
                            "required": true,
                            "type": "file"
                        }
                    ],
                    "type": "File",
                    "responseMessages": [
                        {
                            "code": 201,
                            "message": "Request succeeded"
                        }
                    ]
                },

In the swagger js client the method ends up being as shown here:

swagger.apis.files.uploadFile(...)

It’s just unclear to me what to pass in as the arguments. I imagine is some combination of a the output of fs.readFileSync() and setting a requestContentType header to multipart/form-data, but so far I haven’t been able to get the data sent to the server.

Any ideas? I haven’t seen any examples of this yet through extensive searching.

Thanks! It’s the missing link in our recently publishes Swagger docs.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
thiagobustamantecommented, May 12, 2017

I’m having problems to implement file uploads in node js.

I have the following swagger 2.0:

  /middleware/authentication/strategies:
    post:
      operationId: MiddlewareRestAddAuthStrategy
      responses:
        '204':
          description: No content
      description: ''
      tags:
        - Middleware
      security:
        - Bearer: []
      consumes:
        - multipart/form-data
      parameters:
        - description: 'Middleware file'
          in: formData
          name: file
          required: true
          type: file
        - description: 'Middleware name'
          in: formData
          name: name
          required: true
          type: string

And I am trying to call this using the following code:

function addAuthStrategy(name, fileName) {
        return new Promise((resolve, reject) => {
            const stream = fs.createReadStream(fileName);
            this.swaggerClient.apis.Middleware.MiddlewareRestAddAuthStrategy({name: name, file: stream})
                .then(response => {
                    if (response.status === 204) {
                        return resolve();
                    }
                    reject(response.text);
                })
                .catch(reject);
        });
}

The request is created to target operation, but the expected parameters (name and file) are both being received as undefined. Not is being sent to the server.

I also tried to change the createReadStreamcall to fs.readFileSync(fileName), but the problem remains.

Am I missing something? Is it the correct way to call this? I could not find any documentation nether a test case. The links to test cases above are broken.

Thanks in advance.

0reactions
dcarrot2commented, Jun 22, 2018

@thiagobustamante Were you able to fix your issue? I’m experiencing the same problem, and from reading the source code, it’s not possible to pass a buffer for a formdata file upload if you’re targeting a swagger 2.0 spec? https://github.com/swagger-api/swagger-js/blob/ba6c94912bb13e7fb2cdc6bc88b348c353a789c8/src/execute/oas3/build-request.js#L71

Edit: looks like the file can be passed in via fs.createReadStream($FILE_PATH)

Read more comments on GitHub >

github_iconTop Results From Across the Web

File uploading in Node.js - GeeksforGeeks
Introduction: File uploading means a user from client machine requests to upload file to the server. For example, users can upload images, ...
Read more >
Node.js file upload example with Ajax and JavaScript
Step-by-step Node.js file upload example · Ensure Node. · Create a file named upload. · Add FileSystem (fs) and Formidable library dependencies ...
Read more >
How to Handle File Uploads from Node.js to Express - Twilio
Learn how to upload files by posting multipart/form-data from Node.js to Express with the help of the FormData and Multer libraries.
Read more >
Multer: Easily upload files with Node.js and Express
Multer is a Node.js middleware for handling multipart/form-data that makes the otherwise painstaking process of uploading files ...
Read more >
How to Implement a File Upload Server with Node.js
We can use the HTML form to upload files, and in the Node.js server, we can use the busboy package to parse incoming...
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