0
0
FastAPIframework~5 mins

Status code control in FastAPI - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of status code control in FastAPI?
Status code control in FastAPI lets you specify the HTTP response code sent back to the client, showing if the request was successful, failed, or something else.
Click to reveal answer
beginner
How do you set a custom status code for a response in a FastAPI path operation?
You can set a custom status code by using the status_code parameter in the path operation decorator, like @app.post('/items/', status_code=201).
Click to reveal answer
beginner
What is the default status code for a successful GET request in FastAPI?
The default status code for a successful GET request in FastAPI is 200 OK.
Click to reveal answer
intermediate
How can you return a different status code dynamically inside a FastAPI path function?
You can return a Response or JSONResponse object with a custom status_code set, or raise an HTTPException with the desired status code.
Click to reveal answer
beginner
What status code should you use in FastAPI when a resource is successfully created?
Use status code 201 Created to indicate a resource was successfully created.
Click to reveal answer
Which status code indicates a successful POST request that created a resource?
A200
B201
C404
D500
How do you specify a status code of 204 (No Content) in a FastAPI route?
AUse <code>status_code=204</code> in the decorator
BReturn <code>None</code> from the function
CRaise <code>HTTPException(status_code=204)</code>
DSet <code>response.status_code = 204</code> inside the function
What happens if you don't specify a status code in a FastAPI GET route?
AFastAPI returns 404 by default
BFastAPI returns 500 by default
CFastAPI returns 200 by default
DFastAPI returns 201 by default
Which FastAPI class can you use to raise an error with a specific status code?
AResponse
BRequest
CJSONResponse
DHTTPException
To return a 404 Not Found error in FastAPI, you should:
ARaise <code>HTTPException(status_code=404)</code>
BReturn <code>None</code>
CSet <code>status_code=404</code> in the decorator
DReturn a string '404'
Explain how to control HTTP status codes in FastAPI path operations.
Think about decorator options and exceptions.
You got /3 concepts.
    Describe when and why you would use status code 201 in FastAPI.
    Consider POST requests creating new items.
    You got /3 concepts.