pythonswaggerfastapiswagger-uimedia-type

How to disable application/json in Swagger UI autodocs of a FastAPI application?


My API can return only a file:

    @router.get(
        "/partners/{partner_id}/rsr-requests/{rsr_request_id}/{document_path}",
        responses={200: {"content": {"application/octet-stream": {}}, "description": "Файл"}}
    )
    async def download_rsr_document(...):
        pass

But in Swagger UI, I can see that application/json still remains. How to disable it?

enter image description here


Solution

  • From the docs you use the response_class parameter with the type of Response you want.

        from fastapi import FastAPI
        from fastapi.responses import FileResponse
        
        some_file_path = "large-video-file.mp4"
        app = FastAPI()
        
        
        @app.get("/", response_class=FileResponse)
        async def main():
            return some_file_path