Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JAVASCRIPT] Added logic to properly process multipart/form-data #908

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

georgedias
Copy link

This PR addresses the issue when uploading a multipart/form-data utilizing a node - javascript client SDK as reported in issue 11000:

File modified (ApiClient.mustache - es5 and es6)

Added the following check in the isFileParam(param) method

    {{#emitJSDoc}}/**
    * Checks whether the given parameter value represents file-like content.
    * @param param The parameter to check.
    * @returns {Boolean} <code>true</code> if <code>param</code> represents a file.
    */
    {{/emitJSDoc}}
    isFileParam(param) {
        // fs.ReadStream in Node.js and Electron (but not in runtime like browserify)
        if (typeof require === 'function') {
            let fs;
            try {
                fs = require('fs');
            } catch (err) {}
            if (fs && fs.ReadStream && param instanceof fs.ReadStream) {
                return true;
            }
        }

        // HACK (issue #11000)
        // Readstream - fs.createReadStream or fs.ReadStream
        if (isStream.readable(param)) {
            return true;
        }
                
        // Buffer in Node.js
        if (typeof Buffer === 'function' && param instanceof Buffer) {
            return true;
        }

        // Blob in browser
        if (typeof Blob === 'function' && param instanceof Blob) {
            return true;
        }

        // File in browser (it seems File object is also instance of Blob, but keep this for safe)
        if (typeof File === 'function' && param instanceof File) {
            return true;
        }

        return false;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant