Skip to content

No form generated in swagger docs page for FromText, FromFiles #546

@yinkaisheng

Description

@yinkaisheng
# -*- coding: utf-8 -*-
import blacksheep as bs
from blacksheep.server.openapi.v3 import OpenAPIHandler
from openapidocs.v3 import Info
import uvicorn
from loguru import logger

app = bs.Application(show_error_details=True)
docs = OpenAPIHandler(info=Info(title="Example API", version="0.0.1"))
docs.bind_app(app)

@bs.post("/upload")
async def api_upload(req: bs.Request, text: bs.FromText, file: bs.FromFiles):
    logger.info(f'from {req.client_ip} url={req.url} text={text.value} file={file.value}')
    return 'ok'

if __name__ == '__main__':   
    uvicorn.run(app, host='0.0.0.0', port=8021)

I try to use blacksheep-2.1.0. When I run the above code and open /docs, I see
Image
Won't it generate a form for the params text and file?

Then I use Postman to send the request.

Image

Image
The text.value and file.value contain boundary. Do I need to parse it manually?
It seems that FromText is the whole request body, including the file part.
I have already checked the documentation at https://www.neoteroi.dev/blacksheep/requests, but I didn't find the usage.

If I use fastapi, the usage is simple

@app.post("/upload")
async def api_upload(req: fastapi.Request,
                     text: str = fastapi.Form(..., example="hello"),
                     file: fastapi.UploadFile = fastapi.File(..., example="example.py")):
    logger.info(f'from {req.client.host} url={req.url} text={text} file={file.filename}')
    data = await file.read()
    return 'ok'

Then I can test it in docs page without Postman.
Image

How should I use BlackSheep to create an upload example similar to the one in FastAPI?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions