plangrid/flask-rebar

Support for binary / file types

jjbskir opened this issue · 1 comments

I'm trying to figure out if there is currently support in rebar for doing a POST to upload a file / binary data?

I've been following flask's document for file uploads and created a simple rebar example.

class Request(Schema):
    file = fields.String()

@registry.handles(
    rule="/upload-file",
    method="POST",
    request_body_schema=Request(),
    tags=["main"],
)
def upload_file():
    file = request.files['file']

We can then make a request to this API like

import requests
import io

data = {
    "file": io.BytesIO(b"abc"),
}
requests.post(f"https://localhost:/.../api/upload-file", files=data)

But I get the error
{'message': "Only payloads with 'content-type' 'application/json' are supported."}

The headers of the request are 'Content-Type': 'multipart/form-data' which makes sense, given that the upload was for binary data. Should I be defining rebar differently? It seems to work if I completely get rid of the request_body_schema, since then it's not doing any validation. Swagger does support file type definitions, so there should be some way to define them within flask / rebar.

Unfortunately, currently, if the request_body_schema is specified, it must conform to a application/json content-type.

if request_body_schema:
g.validated_body = get_json_body_params_or_400(schema=request_body_schema)

What you would do in the case where you wanted to use a multipart/form-data, is not specify the request_body_schema, and validate it inside of the request instead.