Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for binary / file types #282

Open
jjbskir opened this issue Jan 12, 2023 · 1 comment
Open

Support for binary / file types #282

jjbskir opened this issue Jan 12, 2023 · 1 comment

Comments

@jjbskir
Copy link

jjbskir commented Jan 12, 2023

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.

@gtmanfred
Copy link
Contributor

gtmanfred commented Jan 17, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants