Challenge - 5 Problems
Message Mastery in FastAPI
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this FastAPI endpoint when sending a POST request?
Consider this FastAPI endpoint that receives a JSON message and returns a confirmation.
What will be the JSON response if the client sends {"message": "Hello"}?
What will be the JSON response if the client sends {"message": "Hello"}?
FastAPI
from fastapi import FastAPI, Request from fastapi.responses import JSONResponse app = FastAPI() @app.post("/send") async def send_message(request: Request): data = await request.json() msg = data.get("message", "") return JSONResponse(content={"status": "received", "message": msg})
Attempts:
2 left
💡 Hint
Look at how the endpoint reads the JSON and what it returns in the response.
✗ Incorrect
The endpoint reads the JSON body, extracts the 'message' key, and returns a JSON with 'status' and 'message'. So the response includes both keys with the sent message.
❓ state_output
intermediate2:00remaining
What is the value of 'messages' after these requests?
Given this FastAPI app that stores messages in a list, what will be the content of 'messages' after two POST requests with messages "Hi" and "Bye"?
FastAPI
from fastapi import FastAPI, Request app = FastAPI() messages = [] @app.post("/chat") async def chat(request: Request): data = await request.json() messages.append(data.get("message", "")) return {"count": len(messages)}
Attempts:
2 left
💡 Hint
The list 'messages' is global and appends each message received.
✗ Incorrect
Each POST request appends the message to the global list. After two requests with "Hi" and "Bye", the list contains both in order.
📝 Syntax
advanced2:00remaining
Which option causes a syntax error in this FastAPI message handler?
Identify which code snippet will cause a syntax error when defining a FastAPI POST endpoint that receives JSON.
Attempts:
2 left
💡 Hint
Check the function parameter syntax carefully.
✗ Incorrect
Option A misses the colon ':' between 'request' and 'Request' in the function parameter, causing a syntax error.
🔧 Debug
advanced2:00remaining
Which option will cause a runtime error when sending a message?
Given these FastAPI endpoints, which one will raise an error when a POST request with JSON {"message": "Hello"} is sent?
Attempts:
2 left
💡 Hint
Consider how JSON data is accessed in Python dictionaries.
✗ Incorrect
JSON data parsed by request.json() returns a dict, which requires key access with brackets []. Using dot notation like data.message causes an AttributeError.
🧠 Conceptual
expert3:00remaining
What is the correct way to handle concurrent message storage in FastAPI?
You want to store messages received via POST requests in a list safely when multiple clients send messages at the same time. Which approach ensures thread-safe updates?
Attempts:
2 left
💡 Hint
Think about concurrency and shared data safety in async web servers.
✗ Incorrect
FastAPI runs asynchronously and can handle multiple requests concurrently. Directly appending to a shared list without locks can cause race conditions. Using an asyncio.Lock ensures safe concurrent access.