0
0
FastAPIframework~10 mins

Accepting connections 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'
AFastAPI
BRequest
CResponse
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 route at the root path that returns a welcome message.

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

Fix the error in the code to run the FastAPI app with uvicorn on port 8000.

FastAPI
import uvicorn

if __name__ == "__main__":
    uvicorn.run("main:[1]", host="127.0.0.1", port=8000, reload=True)
Drag options to blanks, or click blank then click option'
Aapi
Bapp
Capplication
Dserver
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong instance name like server or api
Forgetting the colon separator
4fill in blank
hard

Fill both blanks to create a POST route at '/submit' that accepts JSON data and returns it.

FastAPI
@app.[1]("/submit")
async def submit_data(data: [2]):
    return data
Drag options to blanks, or click blank then click option'
Apost
Bget
Cdict
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST for data submission
Using list instead of dict for JSON data
5fill in blank
hard

Fill all three blanks to create a FastAPI app that accepts connections on all interfaces at port 8080 with reload enabled.

FastAPI
import uvicorn

if __name__ == "__main__":
    uvicorn.run("main:[1]", host="[2]", port=[3], reload=True)
Drag options to blanks, or click blank then click option'
Aapp
B0.0.0.0
C8080
D127.0.0.1
Attempts:
3 left
💡 Hint
Common Mistakes
Using localhost (127.0.0.1) instead of 0.0.0.0 to accept external connections
Using wrong port number