Automatic status code and response type detection
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from fastapi import FastAPI, Response
app = FastAPI()
@app.get("/{option}")
def read_root(option: int):
if option == 1:
return Response(status_code=200)
if option == 2:
return Response(status_code=201)
return Response(status_code=204)
Description
- Open de browser and go to
localhost:8000/docs
- Docs do not show correct response status codes:
Wanted Solution
I would like that FastAPI
could automatically detect Response
status code and response type
Wanted Code
# Using the same code above but it produces correct documented endpoint
Alternatives
No response
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.78.0
Python Version
3.10.4
Additional Context
No response
Issue Analytics
- State:
- Created a year ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Rest API Response Codes And Types Of Rest Requests
In this tutorial, we will learn about different REST response codes, types of REST Requests, and some best practices to be followed.
Read more >Response Status Code - FastAPI
300 and above are for "Redirection". Responses with these status codes may or may not have a body, except for 304 , "Not...
Read more >HTTP/1.1: Status Code Definitions
This class of status code indicates that the client's request was successfully received, understood, and accepted. 10.2.1 200 OK. The request has succeeded....
Read more >What Is Endpoint Detection and Response? | EDR Security
Automated response. Pre-configured rules in an EDR solution can recognize when incoming data indicates a known type of security breach and triggers an...
Read more >The 6 Types of HTTP Status Codes Explained - DYNO Mapper
This status response indicates that the entity making the request has asked the server to change protocols and the server has acquiesce to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I can tell you how to implement this, but it’s not trivial… 🤷♂️
I can tell you for sure that this will never be merged on FastAPI, because it involves doing static type analysis, which is out of scope. Also, it’s a lot of work, and it can be an external package.
How you can implement this:
Response
you want… and that can be recursive as you can have a function calling another function and only then having areturn Response
…Response
s from each endpoint function. Maybe some dynamic programing on functions and possible responses can speed up a bit if the source code is huge (may not be an issue now).In any case… I’m going to work on this idea at some point, mainly for
HTTPException
s tho. I already have this: https://github.com/Kludex/fastapi-responses, but it doesn’t follow the instructions above, it currently implements a very naive approach.It’s highly unlikely this will ever come to be, as it is pretty hard to know what kind of output types an arbitrary function can have.