flask-rebar
flask-rebar copied to clipboard
Support for binary / file types
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.
https://github.com/plangrid/flask-rebar/blob/da6b1638f5e7d114fdccc71c576f89ec54836c79/flask_rebar/rebar.py#L129-L130
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.