Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or Response instead of FastAPI.
Forgetting to call FastAPI() to create the app.
✗ Incorrect
The FastAPI class is imported and used to create the app instance.
2fill in blank
mediumComplete the code to define a GET endpoint that returns JSON.
FastAPI
@app.get("/items/{item_id}") async def read_item(item_id: int): return [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a plain string instead of a dict.
Returning just the integer item_id.
✗ Incorrect
The endpoint returns a dictionary which FastAPI converts to JSON.
3fill in blank
hardFix the error in the response type by completing the code to return a plain text response.
FastAPI
from fastapi.responses import [1] @app.get("/plaintext") async def get_plaintext(): return [1]("Hello World")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using JSONResponse which returns JSON.
Using HTMLResponse which returns HTML content.
✗ Incorrect
Use PlainTextResponse to return plain text content.
4fill in blank
hardFill both blanks to return an HTML response with a custom status code.
FastAPI
from fastapi.responses import [1] @app.get("/custom_html") async def custom_html(): content = "<h1>Welcome</h1>" return [1](content=content, status_code=[2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using PlainTextResponse for HTML content.
Using status code 200 when a custom code is needed.
✗ Incorrect
Use HTMLResponse to return HTML content and set status_code=404 for custom status.
5fill in blank
hardFill all three blanks to return a JSON response with a custom header and status code.
FastAPI
from fastapi.responses import [1] @app.get("/custom_json") async def custom_json(): data = {"message": "Success"} headers = [2] return [1](content=data, status_code=[3], headers=headers)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using PlainTextResponse instead of JSONResponse.
Passing headers as a string instead of a dictionary.
✗ Incorrect
Use JSONResponse to return JSON data, add custom headers as a dict, and set status code 201 for Created.