0
0
FastAPIframework~20 mins

Status code control in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Status Code Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What status code does this FastAPI endpoint return?
Consider this FastAPI endpoint code. What HTTP status code will the client receive when calling /items/42?
FastAPI
from fastapi import FastAPI
from fastapi.responses import JSONResponse

app = FastAPI()

@app.get('/items/{item_id}')
async def read_item(item_id: int):
    if item_id == 42:
        return JSONResponse(content={'item_id': item_id}, status_code=202)
    return {'item_id': item_id}
A200
B500
C404
D202
Attempts:
2 left
💡 Hint
Look at the status_code parameter in JSONResponse for item_id 42.
📝 Syntax
intermediate
2:00remaining
Which option correctly sets a 201 status code for a POST endpoint?
You want to create a FastAPI POST endpoint that returns status code 201 when a new item is created. Which code snippet correctly sets this status code?
A
@app.post('/items')
async def create_item(item: dict):
    return Response(content=item, status_code=201)
B
@app.post('/items')
async def create_item(item: dict):
    return JSONResponse(content=item, status_code=201)
C
@app.post('/items', status_code=201)
async def create_item(item: dict):
    return item
D
@app.post('/items')
async def create_item(item: dict):
    return {'item': item, 'status': 201}
Attempts:
2 left
💡 Hint
Check how FastAPI allows setting status codes directly in the decorator.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI endpoint always return status 200 instead of 404?
Examine the code below. The intention is to return 404 if the item is not found. Why does it always return 200?
FastAPI
from fastapi import FastAPI, HTTPException

app = FastAPI()

items = {1: 'apple', 2: 'banana'}

@app.get('/items/{item_id}')
async def get_item(item_id: int):
    if item_id not in items:
        raise HTTPException(status_code=404, detail='Item not found')
    return {'item': items[item_id]}
AHTTPException must be raised, not returned, to trigger the error response.
BThe status_code 404 is invalid and defaults to 200.
CThe items dictionary is empty, so the condition never triggers.
DFastAPI does not support HTTPException for status control.
Attempts:
2 left
💡 Hint
Think about how exceptions work in Python and FastAPI.
state_output
advanced
2:00remaining
What is the status code and response body when this FastAPI endpoint is called with /users/0?
Analyze the following FastAPI endpoint. What status code and JSON response will the client receive when requesting /users/0?
FastAPI
from fastapi import FastAPI, Response

app = FastAPI()

@app.get('/users/{user_id}')
async def get_user(user_id: int):
    if user_id == 0:
        return Response(content='User not found', status_code=404, media_type='text/plain')
    return {'user_id': user_id}
AStatus code 404 with plain text body 'User not found'
BStatus code 200 with JSON body {'user_id': 0}
CStatus code 404 with JSON body {'detail': 'User not found'}
DStatus code 500 with empty body
Attempts:
2 left
💡 Hint
Look at the Response object and its parameters.
🧠 Conceptual
expert
2:00remaining
Which statement about FastAPI status code control is true?
Select the correct statement about how FastAPI handles HTTP status codes in endpoint responses.
AReturning a dictionary from an endpoint always results in status code 500 unless a status_code is set explicitly.
BSetting status_code in the decorator changes the default response status code unless overridden by returning a Response with a different status_code.
CFastAPI automatically sets status code 201 for all POST requests regardless of decorator or return value.
DRaising HTTPException changes the response body but does not affect the HTTP status code sent to the client.
Attempts:
2 left
💡 Hint
Think about how decorator status_code and return values interact.