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.

Multipart requests

See original GitHub issue

Hello!

I did not found standard way to describe a multipart request. Is it supported?

Currently i had to write something like this to let uploaded files work:

request_body_description = {
    "content": {
        "multipart/form-data": {
            "schema": {
                "type": "object",
                "properties": {"logo": {"type": "string", "format": "binary"}},
            }
        }
    }
}


@bp.route("/update-logo")
class UpdateLogo(MethodView):
    @bp.response(code=204)
    @bp.doc(requestBody=request_body_description)
    def post(self):
        file = flask.request.files["logo"]
        filename = secure_filename(file.filename)
        binary = file.read()
        
        ... do something with file ...

This code allows swagger to render input type=“file” with name “logo” and send multipart request when executing request from web interface.

Am i missing something?

Thanks!

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:28 (12 by maintainers)

github_iconTop GitHub Comments

4reactions
lafrechcommented, May 20, 2020

I’ve been banging my head against this a little more. Unfortunately, auto-documenting mixed multipart request bodies is a bit complicated.

There are issues because for instance arguments allows to pass description, but this is a requestBody level attribute, not a part (as in multi-part) level attribute, therefore, what we expect from the following would be undetermined:

        @blp.arguments(FilesSchema, location='files', description='Some description')
        @blp.arguments(FormSchema, location='form', description='Some other description')

This implementation issues make me think that the sanest way to do that would be to require more explicitness from the user.

We could add a multipart_arguments decorator so that we don’t have to detect multipart cases and we can ask for all needed parameters.

Things would go relatively smooth if we could pass apispec a schema that would be the aggregation of the different schemas involved ( a single schema inheriting from all). This could avoid duplicating apispec code, or having to pull functions from there to do the job here.

To be able to do so, we’d need to add a restriction to multipart_argument: only pass schema classes (or perhaps field dicts), not schema instances. Otherwise, modifiers would be lost in the process. I think we can live with this limitation.


I don’t think I’ll do that for the next release.

Once https://github.com/marshmallow-code/flask-smorest/tree/document_multipart_files is merged (almost done, just waiting for apispec 3.0), we’ll be able to send files (using multipart), and to properly set a content type. This is already an improvement.

We shall add a note in the docs saying mixed multipart is not supported.

3reactions
aprilahijriyancommented, Apr 9, 2021

Any progress here? i am currently looking for a way how to work with this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is http multipart request? - Stack Overflow
An HTTP multipart request is an HTTP request that HTTP clients construct to send files and data over to an HTTP Server.
Read more >
A Brief Tour of Multipart Requests - PSPDFKit
The most common use for multipart requests is web forms. When you have a form with only text-based input elements — like text...
Read more >
Multipart Request Handling in Spring - Baeldung
Multipart requests consist of sending data of many different types separated by a boundary as part of a single HTTP method call. Generally,...
Read more >
Multipart requests - Nine Nines
Multipart requests. Multipart originates from MIME, an Internet standard that extends the format of emails. A multipart message is a list of parts....
Read more >
Streaming Multipart Requests - Khanlou
What is a multipart request? Multipart encoding is the de facto way of sending large files over the internet. When you select a...
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