Skip to content

Upload binary files via multipart/form-data from REST API Explorer #2666

@christo911

Description

@christo911

Description / Steps to reproduce / Feature proposal

Unable to use local swagger-ui to upload files (Button missing).
Loopback requestBody does not accept "format: binary" used by openAPI to determine that the input field is for file upload.

Current Behavior

When using upload controller from example, the swagger-ui upload button does not appear and only shows a text box (no Upload button)
However if following openAPI spec, the button shows, but on submission of the file, errors occur unknown format "binary" is used in schema at path "#"

Expected Behavior

Either code should work or?

See Reporting Issues for more tips on writing good issues

From example - Button not showing

export class UploadController {
    @post("/image", {
        responses: {
            200: {
                content: {
                    "application/json": {
                        schema: {
                            type: "object"
                        },
                    },
                },
                description: "",
            },
        },
    })

    async image(
        @requestBody({
            description: 'multipart/form-data value.',
            required: true,
            content: {
                 "multipart/form-data": {
                     // Skip body parsing
                     "x-parser': 'stream",
                     schema: {
                         type: "object",
                     },
                 },
             },
        })
            request: Request,
        @inject(RestBindings.Http.RESPONSE) response: Response,
    ): Promise<object> {
        //const storage = multer.memoryStorage();
        
        const upload = multer({dest: "/"});

        return new Promise<object>((resolve, reject) => {
            upload.any()(request, response, (err: any) => {
                if (err) {
                    reject(err);
                } else {
                   resolve({
                        files: request.body.file,
                        fields: (request as any).fields,
                    });
                }
            });
        });
    }
}

But change the request body to:

@requestBody({
            description: 'multipart/form-data value.',
            required: true,
            content: {
                'multipart/form-data': {
                    'x-parser': 'stream',
                    schema: {
                        type: 'object',
                        properties: {
                            file: {
                                type: 'string',
                                format: 'binary'
                            }
                        }
                    }
                }
            }

        })

the choose file button appears, but on submission I get the error...
Unhandled error in POST /image: 500 Error: unknown format "binary" is used in schema at path "#/properties/file"

A bit lost here... thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    RESTIssues related to @loopback/rest package and REST transport in generalfeaturehelp wanted

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions