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
✗ Incorrect
201 means the resource was created successfully.
How do you specify a status code of 204 (No Content) in a FastAPI route?
AUse <code>status_code=204</code> in the decorator
Hint: Decorator status_code sets fixed code, use exceptions for errors [OK]
Common Mistakes:
Using status_code=404 in decorator to signal missing item
Not raising HTTPException for 404 errors
Assuming status_code in decorator is dynamic
5. You want to create a FastAPI route that returns 201 when a new user is created, but 400 if the username is missing. Which is the best way to control the status codes?
hard
A. Use status_code=201 in decorator and raise HTTPException(status_code=400) if username missing
B. Set status_code=201 in decorator and return JSONResponse with status_code=400 inside function
C. Return a tuple with data and status code 201 or 400
D. Set status_code=400 in decorator and return 201 in response body
Solution
Step 1: Understand status code control methods
Setting status_code=201 in decorator sets default success code. Raising HTTPException sends error with desired status.
Step 2: Evaluate options
Use status_code=201 in decorator and raise HTTPException(status_code=400) if username missing uses decorator for success and exception for error, which is correct. Set status_code=201 in decorator and return JSONResponse with status_code=400 inside function mixes decorator and response status codes causing confusion. Return a tuple with data and status code 201 or 400 returning tuple is invalid in FastAPI. Set status_code=400 in decorator and return 201 in response body sets wrong default status code and misuses response body.
Final Answer:
Use status_code=201 in decorator and raise HTTPException(status_code=400) if username missing -> Option A
Quick Check:
Decorator for success, exceptions for errors [OK]
Hint: Use decorator for success, raise HTTPException for errors [OK]
Common Mistakes:
Returning tuples with status codes (not supported)
Setting error status_code in decorator instead of raising exception
Mixing response status codes inconsistently
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.