0
0
FastAPIframework~10 mins

Sending and receiving messages in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import FastAPI and create an app instance.

FastAPI
from fastapi import [1]

app = [1]()
Drag options to blanks, or click blank then click option'
AResponse
BRequest
CFastAPI
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or Response instead of FastAPI
Not creating an instance of FastAPI
2fill in blank
medium

Complete the code to define a GET endpoint that returns a welcome message.

FastAPI
@app.[1]("/")
async def read_root():
    return {"message": "Hello, FastAPI!"}
Drag options to blanks, or click blank then click option'
Apost
Bget
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using @app.post instead of @app.get
Forgetting the decorator
3fill in blank
hard

Fix the error in the code to receive JSON data in a POST request.

FastAPI
from fastapi import FastAPI, [1]

app = FastAPI()

@app.post("/items")
async def create_item(item = [1]()):
    return item
Drag options to blanks, or click blank then click option'
ABody
BQuery
CPath
DHeader
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query or Path instead of Body
Not importing Body
4fill in blank
hard

Fill both blanks to define a POST endpoint that receives a message and returns it with a status.

FastAPI
from fastapi import FastAPI, [1]
from pydantic import BaseModel

app = FastAPI()

class Message(BaseModel):
    text: str

@app.post("/send")
async def send_message(message: [2] = [1]()):
    return {"status": "received", "message": message.text}
Drag options to blanks, or click blank then click option'
ABody
BQuery
CMessage
DPath
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query or Path instead of Body
Using wrong type annotation for message
5fill in blank
hard

Fill all three blanks to create a WebSocket endpoint that sends back received messages.

FastAPI
from fastapi import FastAPI, WebSocket

app = FastAPI()

@app.websocket("/[1]")
async def websocket_endpoint(websocket: [2]):
    await websocket.accept()
    data = await websocket.[3]()
    await websocket.send_text(f"Message received: {data}")
Drag options to blanks, or click blank then click option'
Aws
BWebSocket
Creceive_text
Dhttp
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'ws' in the path
Wrong parameter type instead of WebSocket
Using receive() instead of receive_text()