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.

Allow model types to be of type=file

See original GitHub issue

Currently only operation parameters can be of type file but in some scenarios you want to have the file as part of your model. In our API we have 1 input model defined for our operation:

"FileUploadRequestViewModel": {
    "required": ["username", "file"],
    "type": "object",
    "properties": {
        "username": {
            "description": "The username of the file owner.",
            "type": "string"
        },
        "description": {
            "description": "A description of the file contents",
            "type": "string"
        },
        "file": {
            "description": "The file to upload.",
            "type": "file"
        }
    }
},

If any model property is of type file, the operation must be called with multipart/form-data. An operation could look like this:

"/v1/files": {
    "post": {
        "summary": "Uploads a new file to the data repository.",
        "operationId": "Files_Upload",
        "consumes": ["multipart/form-data"],
        "produces": ["application/json", "text/json"],
        "parameters": [{
            "name": "request",
            "in": "body",
            "description": "The model describing the file to be uploaded",
            "required": true,
            "schema": {
                "$ref": "#/definitions/FileUploadRequestViewModel"
            }
        }],
        "responses": {
            "200": {
                "description": "The file already existed in the repository and was updated.",
                "schema": {
                    "$ref": "#/definitions/FileUploadResponseViewModel"
                }
            },
            "201": {
                "description": "The file push was successful and it was added as new to the file repository.",
                "schema": {
                    "$ref": "#/definitions/FileUploadResponseViewModel"
                }
            },
            "404": {
                "description": "No user with the given username could be found."
            },
            "415": {
                "description": "If the file push was not called with a multipart mimetype"
            },
            "400": {
                "description": "One or more of the input parameters were invalid, check the body for detailed validation messages.",
                "schema": {
                    "$ref": "#/definitions/HttpError"
                }
            }
        },
        "deprecated": false
    }
},

In the example above, the user would send the following content to upload a file:

POST /v1/files HTTP/1.1
{{Other Headers}}
Content-Type: multipart/form-data; boundary=--389C9546-8F63-47C8-A1D7-51B7D88FDB08
Content-Length: {{Actual Length}}

--389C9546-8F63-47C8-A1D7-51B7D88FDB08
Content-Disposition: form-data; name="request.username"

Username
--389C9546-8F63-47C8-A1D7-51B7D88FDB08
Content-Disposition: form-data; name="request.description"

An archive of my holiday pictures.
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="request.file"; filename="MyHolidayPictures.zip"
Content-Type: application/zip

{{Binary Content}}
--389C9546-8F63-47C8-A1D7-51B7D88FDB08--

As you can see the model properties are accessed via the name of the single multipart boundaries.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
webroncommented, May 19, 2016

Thanks. You’ve given a good idea on how to deal with an existing issue (or a set of issues) and I’ll consider that when approaching it. Need to process it some more, but it may be a way to eliminate formData parameters and provide the extra flexibility that’s missing now. No guarantees though.

0reactions
fehguycommented, Feb 1, 2017

File is dead! Long live File.

See #878

Read more comments on GitHub >

github_iconTop Results From Across the Web

<input type="file"> - HTML: HyperText Markup Language | MDN
elements with type="file" let the user choose one or more files from ... a string that defines the file types the file input...
Read more >
How to Change the Allowed File Types in the File Upload ...
Enter an asterisk in the File Types field to allow all file types. If you upload a file type that's not allowed, the...
Read more >
django - Only accept a certain file type in FileField, server-side
First. Create a file named formatChecker.py inside the app where the you have the model that has the FileField ...
Read more >
How to Allow the File Input Type to Accept Only Image Files
In this tutorial, we'll show how to allow the file input type to accept only image files or certain image file extensions. Use...
Read more >
Accept Only Specific File Types in Django File Upload - Blog
Accept Only Specific File Types in Django There are times that we only have to accept specific file types and limit file size...
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