Challenge - 5 Problems
Status Code Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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}
Attempts:
2 left
💡 Hint
Look at the status_code parameter in JSONResponse for item_id 42.
✗ Incorrect
When item_id is 42, the endpoint returns a JSONResponse with status_code=202. Otherwise, it returns a default 200 response.
📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
Check how FastAPI allows setting status codes directly in the decorator.
✗ Incorrect
Option C uses the status_code parameter in the decorator, which is the recommended way to set the response status code for simple returns.
🔧 Debug
advanced2: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]}
Attempts:
2 left
💡 Hint
Think about how exceptions work in Python and FastAPI.
✗ Incorrect
Returning HTTPException just sends it as a normal response body with status 200. You must raise it to trigger the error status.
❓ state_output
advanced2: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}
Attempts:
2 left
💡 Hint
Look at the Response object and its parameters.
✗ Incorrect
The endpoint returns a Response with status 404 and plain text content 'User not found' when user_id is 0.
🧠 Conceptual
expert2:00remaining
Which statement about FastAPI status code control is true?
Select the correct statement about how FastAPI handles HTTP status codes in endpoint responses.
Attempts:
2 left
💡 Hint
Think about how decorator status_code and return values interact.
✗ Incorrect
The status_code in the decorator sets the default status code. Returning a Response with a different status_code overrides it. Other options are incorrect.