Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.3k
-
First Check
Commit to Help
Example CodefromfastapiimportFastAPI,File,UploadFilefromfastapi.responsesimportFileResponse,Responseapp=FastAPI()classImageResponse(Response):media_type="image/*"# What do I need to add here to specify that 'format = "binary"'# here I would like to no longer have to specify 'responses' but only use 'response_class' as a single source of information@app.post("what_I_currently_do",response_class=ImageResponse,responses={200: {"content": {"image/*": {"schema":{"type":"string","format":"binary" } } }}},)asyncdefsend_g(b:UploadFile=File(...)):return"/tmp/test/b.jpg" DescriptionI created a POST route that return a a binary formatted image and I would like this to be specified in the For that I currently do it by adding the Is it possible? If so, how? Operating SystemLinux Operating System DetailsNo response FastAPI Version0.66.1 Python Version3.9.7 Additional ContextNo response |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 5 comments
-
You can try StreamingResponse: |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
The They seem related but they aren't really. To demonstrate how the params importtypingfromfastapiimportBackgroundTasks,FastAPI,File,UploadFilefromfastapi.responsesimportFileResponse,Response,JSONResponsefromstarlette.backgroundimportBackgroundTaskimportuvicornapp=FastAPI()@app.get("/with_direct_jsonresponse")asyncdefwith_direct_jsonresponse():returnJSONResponse(content="{'hello':'world']",status_code=408)# 408 will not be documented! Will only show a 200 as per default response class@app.get("with_direct_jsonresponse_and_responses",responses={406:{"description":"in-line 406 response wut?"}})asyncdefwith_direct_jsonresponse_and_responses():returnJSONResponse(content="{'hello':'world']")# This would document both 200 (as per default response class) and 406classCustomJSONResponseWith407(JSONResponse):media_type="application/json"def__init__(self,content:typing.Any,status_code:int=407,headers:typing.Optional[dict]=None,media_type:typing.Optional[str]=None,background:typing.Optional[BackgroundTask]=None, )->None:super().__init__(content,status_code,headers,media_type,background)@app.get("/with_response_class",response_class=CustomJSONResponseWith407)asyncdefwith_response_class():return {"is this":"a 407?"}# documents only a 407, as the response class is set to that.@app.get("/with_response_class_and_responses",response_class=CustomJSONResponseWith407,responses={406:{"description":"in-line 406 response wut?"}})asyncdefwith_response_class():return {"is this":"a 407?"}# documents only a 407 as the response class is set to that,# But also a 406.if__name__=="__main__":uvicorn.run(app,host="0.0.0.0",port=8000, ) So, there is no object in the |
BetaWas this translation helpful?Give feedback.
All reactions
-
This wouldn't work. See example below: @app.get("/with_response_class_as_streamingresponse",response_class=StreamingResponse)asyncdefwith_response_class_as_streamingresponse():return {"is this":"a 40x?"} This is because StreamingResponse as a response_class just let's the documentation know that the default response HTTP code is 200 but basically nothing much else. It even defaults the response content-type to |
BetaWas this translation helpful?Give feedback.
All reactions
-
Thank you all for your feedbacks. It sound strange to me to not be able to specify the schema's type within the As a workaround I will specify it in the classImageResponse(Response):media_type="image/*"schema= {"something":"another thing"}@app.post("my_route",response_class=ImageResponse,responses={200: {"content": {"image/*": {"schema":ImageResponse.schema } }}},)asyncdefsend_g(b:UploadFile=File(...)):return"/tmp/test/b.jpg" it's not perfect but it works 🙂 Don't hesitate if you have any suggestion on how to write that in a better way. In the mean time I think we can close this issue. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 3
-
I had a similar need myself, so I found this thread. I dug through the Starlette code and FastAPI code, and as far as I can see, there's no way to specify the schema on the Response class itself - so Ithink you found the minimal code. Here's what I ended up using, to match the schema recommended by the OpenAPI docs:https://swagger.io/docs/specification/describing-responses/#response-that-returns-a-file Full code in: https://github.com/pamelafox/staticmaps-function/blob/main/function/fastapi_routes.py |
BetaWas this translation helpful?Give feedback.
All reactions
This discussion was converted from issue #5034 on February 27, 2023 21:45.
