0
0
FastAPIframework~20 mins

Sending and receiving messages in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Message Mastery in FastAPI
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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"}?
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})
A{"message": "Hello"}
B{"status": "received"}
C{"status": "received", "message": "Hello"}
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Look at how the endpoint reads the JSON and what it returns in the response.
state_output
intermediate
2: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)}
A["Hi", "Bye"]
B["Bye", "Hi"]
C[]
D["Hi"]
Attempts:
2 left
💡 Hint
The list 'messages' is global and appends each message received.
📝 Syntax
advanced
2: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.
A
async def receive(request Request):
    data = await request.json()
    return {"msg": data["message"]}
B
async def receive(request: Request):
    data = await request.json()
    return {"msg": data["message"]}
C
async def receive(request: Request):
    data = await request.json()
    return {"msg": data.get("message")}
D
}]"egassem"[atad :"gsm"{ nruter    
)(nosj.tseuqer tiawa = atad    
:)tseuqeR :tseuqer(eviecer fed cnysa
Attempts:
2 left
💡 Hint
Check the function parameter syntax carefully.
🔧 Debug
advanced
2: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?
A
async def send(request: Request):
    data = await request.json()
    return {"msg": data["message"]}
B
async def send(request: Request):
    data = await request.json()
    return {"msg": data.message}
C
async def send(request: Request):
    data = await request.json()
    return {"msg": data.get("message", "")}
D
async def send(request: Request):
    data = await request.json()
    return {"msg": data.get("message")}
Attempts:
2 left
💡 Hint
Consider how JSON data is accessed in Python dictionaries.
🧠 Conceptual
expert
3: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?
AUse a global list and rely on FastAPI's single-threaded nature to avoid conflicts.
BUse a global list and append messages directly without locks.
CUse a local list inside the endpoint function to store messages temporarily.
DUse a global list and protect append operations with an asyncio.Lock.
Attempts:
2 left
💡 Hint
Think about concurrency and shared data safety in async web servers.